SELECT ORDERED statement (select_ordered_statement:_positioned) 

The SELECT ORDERED statement ( select_ordered_statement:_positioned ) selects the first or last row, or, in relation to a certain position, the next or previous row in an ordered table. The order is defined by a key or by an index. The position is defined by a cursor position.

Syntax

<select_ordered_statement:_positioned> ::=
<select_ordered_format1:_positioned> | <select_ordered_format2:_positioned>

<select_ordered_format1:_positioned> ::=
  SELECT <FIRST | LAST> <select_column>,... INTO <parameter_spec>,...
FROM <table_name> [INDEX <column_name> | INDEXNAME <index_name>]
WHERE CURRENT OF <result_table_name> [<lock_option>]
| SELECT <FIRST | LAST> <select_column>,... INTO <parameter_spec>,...
FROM <table_name> [index_pos_spec]
WHERE CURRENT OF <result_table_name> [<lock_option>]

<select_ordered_format2:_positioned> ::=
SELECT <NEXT | PREV> <select_column>,...INTO <parameter_spec>,...
FROM <table_name>
[<index _ pos _ spec>]
WHERE CURRENT OF <result_table_name> [<lock_option>]

select_column, parameter_spec, table_name, column_name, index_name, result_table_name, lock_option, index_pos_spec

Explanation

The select ordered statement is used to access the first or last row of an order defined by the key or a secondary key, or to access the previous or next row starting at a specified position.

The result table must have been specified with FOR UPDATE.

The user must have the SELECT privilege for the selected columns or for the entire table.

The table name in the SELECT ORDERED statement must be identical to that in the FROM clause of the QUERY statement that generated the result table.

The position within the table is defined by the optional index position specification and by a key value, whereby the key value is determined by the cursor position.

FIRST | LAST

FIRST (LAST) produces a search for the first (last) row which, in relation to the order, is greater (less) than or equal to the position.

NEXT | PREV

NEXT (PREV) produces a search in ascending (descending) order for the next row, starting at the specified position.

Single-column index

If an index name is specified with INDEX <column_name> or INDEXNAME <index_name> or if an index position is specified and the index associated with it only contains one column, the rows with NULL values in the index column are not taken into account for the SELECT ORDERED statement. In such a case, the result of the select ordered statement can never be a row with a NULL value in the index column.

Further information

INTO <parameter_spec> is not necessary in interactive mode.

The column name must denote an indexed column.

The LOCK option determines which lock is set for the read row.

A LONG column can only be specified in a selected column in the uppermost sequence of selected columns in this SELECT ORDERED statement.

Result

If the cursor is positioned on a row of the result table and a row was found which satisfies the specified conditions, then the corresponding column values are assigned to the parameters. The FETCH statement rules apply for assigning the values to the parameters.

If the cursor is not positioned on a row in the result table, an error message is issued and no values are assigned to the parameters specified in the parameter specifications.

See also:

SELECT ORDERED statement (select_ordered_statement:_searched)