Figure: Deriving a cursor table shows the sequence of rows in the cursor table if the cursor declaration is specified as
in Example 3. In Figure: Deriving a cursor table, the value of ITMNO is different for each row, so the sequence of rows
in the cursor table is unique. The situation when the value of ITMNO is the same for more than one row is explained
further on.
The sort specification determines the sequence of rows that have different values for the sort key. However, if more
than one row has the same value for the sort key, the sequence of rows cannot be determined. To reorder rows that
have the same value for the sort key, specify multiple sort keys separated by commas. Multiple sort keys specified in
the ORDER BY clause are called the first sort key, the second sort key, and so on. Rows in the cursor table are first
reordered by the first sort key, then rows that have the same value for the first sort key are reordered by the second
sort key. The sequence of rows that have the same values for all specified sort keys cannot be determined. The
following is an example of specifying sort keys:
Example 4:
In this example, the cursor declaration specifies that data is fetched by cursor in the sequence from
lowest WHCODE.
[Figure: Example of specifying multiple sort keys]
When rows are to be reordered, the names of columns used as sort keys must be specified. However, columns
containing the results of operational expressions or set functions specified in a select column list in a query
specification do not have names. To use a column without a name as a sort key, specify a column number instead of a
column name in the sort key specification. Columns are numbered in sequence from the left in the cursor table. The
following is an example of specifying column numbers:
Example 5:
47