The SELECT function combines, filters, and transforms complex message and database data.
The return value of the SELECT function is typically another message tree that contains "rows" whose structure and content is determined by the SelectClause. The number of rows in the result is the sum of all the "rows" pointed to by all the field references and table references in the FROM clause, filtered by the WHERE clause; only those fields for which the WHERE clause evaluates to TRUE are included.
The return value of the SELECT function can also be scalar (see ITEM selections).
Typically, both the SelectClause and the WHERE clause expressions use the correlation variable to access "column" values (that is, fields in the input message tree) and thus to build a new message tree containing data from the input message. The correlation variable is referred to by the name specified in the AS CorrelationName clause or, if an AS clause is not specified, by the final name in the FROM FieldReference (that is, the name after the last dot).
If the field reference is a actually a TableReference, the operation is very similar. In this case, the input is a real database table and is thus restricted to the flat structures supported by databases. The result tree is still not so restricted, however.
If there is more than one field reference in the FROM clause, the rightmost reference steps through each of its rows for each row in the next-to-rightmost reference, and so on. The total number of rows in the result is thus the product of the number of rows in each table. Such selects are known as joins and commonly use a WHERE clause that excludes most of these rows from the result. Joins are commonly used to add database data to messages.
The following example performs a SELECT on the table "Parts" in the schema "Shop" in the database "DSN1". Because there is no WHERE clause, all rows are selected. Because the select clause expressions (for example, P.PartNumber) contain no AS clauses, the fields in the result adopt the same names:
SET PartsTable.Part[] = SELECT P.PartNumber, P.Description, P.Price FROM Database.DSN1.Shop.Parts AS P;
If the target of the SET statement ("PartsTable") is a variable of type ROW, after the statement is executed PartsTable will have, as children of its root element, a field called "Part" for each row in the table. Each of the "Part" fields will have child fields called "PartNumber", "Description", and "Price". The child fields will have values dictated by the contents of the table. ("PartsTable" could also be a reference into a message tree).
The next example performs a similar SELECT. This case differs from the last in that the SELECT is performed on the message tree produced by the first example (rather than on a real database table). The result is assigned into a subfolder of "OutputRoot":
SET OutputRoot.XML.Data.TableData.Part[] = SELECT P.PartNumber, P.Description, P.Price FROM PartsTable.Part[] AS P;
The INSERT clause is an alternative to the AS clause. It assigns the result of the SelectClause expression (which must be a row) to the current new row itself, rather than to a child of it. The effect of this is to merge the row result of the expression into the row being generated by the SELECT. This differs from the AS clause, in that the AS clause always generates at least one child element before adding a result, whereas INSERT generates none. INSERT is useful when inserting data from other SELECT operations, because it allows the data to be merged without extra folders.
The SelectClause can consist of one of the functions COUNT, MAX, MIN, and SUM operating on an expression. These functions are known as column functions. They return a single scalar value (not a list) giving the count, maximum, minimum, or sum of the values that Expression evaluated to in stepping through the rows of the FROM clause. If Expression evaluates to NULL for a particular row, the value is ignored, so that the function returns the count, maximum, minimum, or sum of the remaining rows.
For the COUNT function only, Expression can consist of a single star (*). This form counts the rows regardless of null values.
To make the result a useful reflection of the input message, Expression typically includes the correlation variable.
Typically, Expression evaluates to the same data type for each row. In these cases, the result of the MAX, MIN, and SUM functions will be of the same data type as the operands. The returned values are not required to be all of the same type, however, and, if they are not, the normal rules of arithmetic apply. For example, if a field in a repeated message structure contains integer values for some rows and float values for others, the sum follows the normal rules for addition. It would be of type float because the operation is equivalent to adding a number of integer and float values.
The result of the COUNT function is always an integer.
FROM expressions in which a correlation variable represents a row in a message behave slightly differently from those in which the correlation variable represents a row in a real database table.
In the message case, a path involving a star (*) has the normal meaning; it ignores the field's name and finds the first field that matches the other criteria (if any).
SELECT * FROM Database.Datasource.SchemaName.Table As A SELECT A.* FROM Database.Datasource.SchemaName.Table As A SELECT A FROM Database.Datasource.SchemaName.Table AS A
Any expressions in these paths can also use any of the broker’s operators and functions in any combination. The expressions can refer to the tables’ columns, message fields, correlation names declared by containing SELECTs, and any declared variables or constants.
FROM clause FieldReferences can contain expressions of any kind (for example, Database.{DataSource}.{Schema}.Table1).
You can calculate a field, data source, schema, or table name at run time.
The WHERE clause expression can use any of the broker’s operators and functions in any combination. It can refer to table columns, message fields, and any declared variables or constants.
However, be aware that the broker treats the WHERE clause expression by examining the expression and deciding whether the whole expression can be evaluated by the database. If it can, it is given to the database. In order to be evaluated by the database, it must use only those functions and operators supported by the database.
The WHERE clause can, however, refer to message fields, correlation names declared by containing SELECTs, and to any other declared variables or constants within scope.
You can use the function THE (which returns the first element of a list) in conjunction with SELECT to produce a non-list result. This is useful, for example, when a SELECT query is required to return no more than one item. It is particularly useful in conjunction with ITEM (see ITEM selections).
Notices |
Trademarks |
Downloads |
Library |
Support |
Feedback
![]() ![]() |
ak05620_ |