Adding Computed Values
[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.]
Computed values in Microsoft code name “M” return the results of a query on one or more underlying extents. Computed values can optionally accept parameters that alter the behavior of the computed value. Note that computed values do not store any data; they only return data from other extents. In this way, they behave similarly to database views or functions.
There are a variety of uses for computed values in “M”. One of the most common uses is to return an aggregation of fields from multiple extents. This prevents the user from having to write the joins for common queries.
Defining Computed Values
Consider the following Hometowns
extent in the Contact
module. This simplified extent contains three fields: Id
, City
, and State
.
module Contact
{
Hometowns :
{(
{
Id : Integer64 => AutoNumber;
City : Text;
State : Text where value.Count == 2;
}
)*} where identity Id;
}
If users need to view only those cities that are in a specific state, an “M” computed value can accept a state abbreviation and return a list of cities. The following code shows an “M” computed value named HometownsByState
that accomplishes this goal.
module Contact
{
HometownsByState ( FilterState : Text ) : {{ Id: Integer64; City: Text; State: Text;}*}
{
Hometowns where value.State == FilterState
select
{
Id => value.Id,
City => value.City,
State => value.State
}
}
}
A computed value declaration begins with the computed value name followed by a list of parameters in parentheses. The HometownsByState
takes a single parameter, FilterState
. Then you describe field names and types that this computed value returns. In this example, the field names and types happen to match those of the Hometowns
extent. The body of the computed value contains the query. In this case, the Hometowns
extent is queried and filtered by the FilterState
parameter. The query assigns each return field of the computed value to a value in the query.
For more information about “M” computed values, see Computed values ("M" Programming Guide).