SELECT ORDERED statement (select_ordered_statement:_searched)
The SELECT ORDERED statement (
select_ordered_statement:_searched ) 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 specified key values and index values.Syntax
<select_ordered_statement:_searched> ::=
<select_ordered_format1:_searched> | <select_ordered_format2:_searched>
<select_ordered_format1:_searched> ::= SELECT <FIRST | LAST> <select_column>,...
INTO <parameter_spec>,... FROM <table_name>
[<pos_spec>] [<where_clause>] [<lock_option>]
<select_ordered_format2:_searched> ::= SELECT <NEXT | PREV> <select_column>,...
INTO <parameter_spec>,... FROM <table_name>
<pos
_ spec> ::= INDEX <column _ name> | INDEXNAME <index _ name>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. For tables defined without key columns, there is the implicitly generated column SYSKEY CHAR(8) BYTE which contains a key generated by the database system. The table column SYSKEY, therefore, can be used in the select ORDERED statement to access a specific table row. In a table, the order defined by the ascending values of SYSKEY corresponds to the order of insertions made to the table.
The position in the table can be specified explicitly by means of an index position and key (
key_spec ). The table does not have to contain a row with the position values.FIRST | LAST
FIRST (LAST) produces a search for the first (last) row in the ordered table which satisfies the specified
WHERE condition and 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 which satisfies the specified WHERE condition, starting at the specified position. If no WHERE condition is specified, the result is the row which is next according to order and 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. This state is indicated by a warning.Further information
The user must have the SELECT privilege for the selected columns or for the entire table.
The select ordered statement cannot be used for view tables which have been defined by SELECT DISTINCT or which have more than one underlying base table.
The column name in the position specification (
pos_spec ) must denote an indexed column.INTO <parameter_spec>
is not necessary in interactive mode.A
LONG column can only be specified in a selected column in the uppermost sequence of selected columns in a SELECT ORDERED statement.The
LOCK option determines which lock is set for the read row.Result
If a row was found that satisfies the specified conditions, the corresponding column values are assigned to the parameters. The
FETCH statement rules apply for assigning the values to the parameters.If the specified table does not contain a row that satisfies the specified conditions, the return code
100 - ROW NOT FOUND - is issued and no values are assigned to the parameters specified in the parameter specifications.See also:
SELECT ORDERED statement (select_ordered_statement:_positioned)