Compartir a través de


Implicit Conversion (M to SQL Mapping)

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

Microsoft code name “M” allows logical values to intermingle with other scalars, but SQL does not support that. Logical expressions like == cannot be stored directly in a table, returned as a scalar or as the result of a query. “M” uses bit for that instead, so the result of == is translated into a "1" or "0." Likewise, logical values stored in tables as bits cannot be used directly in logical expressions like NOT, AND, and OR (or where clauses of queries), so “M” uses expr == 1. Where necessary, expression conversion automatically converts between these two, as shown in the following table.

Operator “M” Example SQL Example

Scalar -> Logical

module M {
    type Person { Name : Text; IsOld : Logical; }
    People : Person*;
    OldPeople() { People where value.IsOld }
}
create table [M].[People]
(
  [IsOld] bit not null,
  [Name] nvarchar(max) not null
);
 
create view [M].[OldPeople]
as
  select [$value].[IsOld] as [IsOld], [$value].[Name] as [Name]
  from [M].[People] as [$value]
  where [$value].[IsOld] = 1;

Scalar -> Logical

module M {
    NegateIt(x : Logical) : Logical { ! x }
}
create function [M].[NegateIt]
(
  @x as bit
)
returns bit  as
  begin
    return casewhen not @x = 1 then 1else 0end
 
  end