ABAP Keyword Documentation → ABAP - Reference → Processing External Data → ABAP - Database Accesses → Open SQL → Open SQL - Read Accesses → SELECT
SELECT - sort_key
Other versions: 7.31 | 7.40 | 7.54
Syntax
... ORDER BY { {PRIMARY KEY}
| { {col1|a1} [ASCENDING|DESCENDING]
{col2|a2} [ASCENDING|DESCENDING] ...}
| (column_syntax) } ... .
Alternatives
1. ... ORDER BY PRIMARY KEY
2. ... ORDER BY {col1|a1} [ASCENDING|DESCENDING]
{col2|a2} [ASCENDING|DESCENDING] ...
3. ... ORDER BY (column_syntax)
Effect
The addition ORDER BY
sorts a multirow results set by the content of the
specified column. The order of the rows in the result set refers to all columns that are not listed
after ORDER BY
, is undefined, and can be different in repeated executions of the same SELECT
statement.
If the addition FOR ALL ENTRIES
is used in the WHERE
condition,
ORDER BY
can only be used with the addition PRIMARY KEY
.
The addition ORDER BY
cannot be used with the addition SINGLE
.
If the addition DISTINCT
is used, you can only specify columns after ORDER BY
that are also listed
after SELECT
. If not, you can also use other columns, as long as there are
no restrictions by other additions such as GROUP BY
.
Notes
- :If a sorted resulting set is assigned to a sorted internal table, the internal table is sorted again according to the sorting instructions.
-
The data is sorted in the database system, once all other actions are completed, such as the definition
of the hitlist using
WHERE
, the calculation of aggregate functions, and grouping usingGROUP BY
. Only the additionUP TO n ROWS
is performed once the hitlist is sorted. -
For performance reasons, a sort should only take place in the database if supported by an index.
Alternative 1
... ORDER BY PRIMARY KEY
Effect
If all columns are specified (by *
after SELECT
), and a single database table is specified after FROM
(rather than a
view or
a join expression), the addition PRIMARY KEY
can be used to sort the resulting set in ascending order according to the content of the
primary key of this database table.
The addition PRIMARY KEY
cannot be specified if a view or a join expression
is statically specified after FROM
. If a view or a join expression is specified
after FROM
in the dynamic specification dbtab_syntax
, the data is sorted by all columns of the resulting set.
Example
Reading the data from database table SFLIGHT for Lufthansa flight 0400, sorted by flight date.
DATA wa_sflight TYPE sflight.
SELECT * FROM sflight
INTO wa_sflight
WHERE carrid = 'LH' AND
connid = '0400'
ORDER BY PRIMARY KEY.
ENDSELECT.
Alternative 2
... ORDER BY {col1|a1} [ASCENDING|DESCENDING]
{col2|a2} [ASCENDING|DESCENDING] ...
Effect
For any column specifications after SELECT
,
a list of columns can be entered after ORDER BY
, by which the results set
should be sorted. Columns can be specified directly using the column names col1
col2 ..., or the alternative column names a1 a2 ...
. The latter is required if you want to sort by columns that are specified as
aggregate expressions. When multiple database tables are accessed and a column name is not unique, the column must be identified using the
column selector ~
.
The additions ASCENDING
and DESCENDING
determine
whether the rows are sorted in ascending or descending order. If neither addition is specified, the
sort is performed in ascending order. The priority of sorting is based on the order in which the components col1 col2...
or a1 a2 ...
are specified.
Pooled and
cluster tables cannot
be sorted by all types of column. Columns specified after ORDER BY
cannot be of the type LCHAR, LRAW, STRING, or RAWSTRING.
Note
If single columns are specified in the addition ORDER BY
, the statement SELECT
uses the
SAP buffering only in the following cases:
- The columns specified are a left-justified subset of the primary key in the correct sequence and no further columns are specified.
- The columns specified represent the whole primary key in the correct sequence. Additional columns that are specified have no influence on the sorting.
In other cases, SAP buffering is ignored.
Example
The rows of database table sflight
are grouped by the columns carrid
and connid
, where for each group the minimum of column seatsocc
is determined. The selection is sorted in ascending order by carrid
and in
descending order by the minimum of occupied seats. The alternative name min
is used for the aggregate expression.
DATA: BEGIN OF wa,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
min TYPE i,
END OF wa.
SELECT carrid connid MIN( seatsocc ) AS min
FROM sflight
INTO CORRESPONDING FIELDS OF wa
GROUP BY carrid connid
ORDER BY carrid min DESCENDING.
WRITE: / wa-carrid, wa-connid, wa-min.
ENDSELECT.
Alternative 3
... ORDER BY (column_syntax)
Effect
As an alternative to static column specification, a parenthesized data object column_syntax
can be specified, which either contains the syntax of the list of columns or is initial when the statement
is executed. The addition PRIMARY KEY
cannot be specified in column_syntax
.
For column_syntax
, the same applies as for the dynamic specification of columns
after SELECT
. If the content of column_syntax
is initial, the addition ORDER BY
is ignored.
Note
The class CL_ABAP_DYN_PRG contains methods that support the creation of correct and secure dynamic column specifications.
Example
Selecting the database table sflight
in a method, whereby the sort criterion is passed as an input parameter. In this case, the user must enter the criterion using the correct syntax on the
selection screen. In a proper application, you would normally prepare an input help using a selection list.
TYPES sflight_table_type TYPE TABLE OF sflight.
CLASS handle_sflight DEFINITION.
PUBLIC SECTION.
CLASS-METHODS select_sort_sflight
IMPORTING sort_crit TYPE string
EXPORTING sflight_tab TYPE sflight_table_type
RAISING cx_sy_dynamic_osql_error.
ENDCLASS.
PARAMETERS p_sort TYPE c LENGTH 40.
DATA: s_sort TYPE string,
result_tab TYPE sflight_table_type.
TRY.
s_sort = p_sort.
handle_sflight=>select_sort_sflight(
EXPORTING sort_crit = s_sort
IMPORTING sflight_tab = result_tab ).
CATCH cx_sy_dynamic_osql_error.
MESSAGE `Wrong sort criterium!` TYPE 'I'.
ENDTRY.
CLASS handle_sflight IMPLEMENTATION.
METHOD select_sort_sflight.
SELECT *
FROM sflight
INTO TABLE sflight_tab
ORDER BY (sort_crit).
ENDMETHOD.
ENDCLASS.