[Figure: Example of rows in a cursor table when the sequence is undefined]
Reordering the sequence of rows
A cursor declaration can specify the reordering, or sorting, of rows in a cursor table. This is done by specifying the
ORDER BY clause after the query expression. The following is an example of specifying the ORDER BY clause:
Example 3:
In this example, the cursor declaration used in Example 2 specifies that data is fetched by the cursor in
the sequence from lowest ITMNO.
Specify the names of the columns used for sorting and the method of sorting in the ORDER BY clause. The columns
used for sorting must be columns contained in the sort table. In other words, only columns derived from the result of
the query expression can be used in sorting. Columns used in sorting are called sort keys. Sorting can be performed
in either ascending (ASC) or descending (DESC) order. If no sort method is specified, the system uses "ASC" as the
default.
ASC:
This reorders values starting with the smallest value. This is called sorting in ascending order.
DESC:
This reorders values starting with the largest value. This is called sorting in descending order.
46