AS ABAP Release 754, ©Copyright 2019 SAP SE. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Reads →
SELECT
Syntax
SELECT mainquery_clauses
[UNION ...]
INTO|APPENDING target
[UP TO ...] [OFFSET ...]
[abap_options].
...
[ENDSELECT].
Effect
Uses the ABAP SQL statement SELECT as a standalone statement. This statement reads data from one or more database tables , classic views, or non-abstract CDS entities, uses this data to create a multiple row or a single row results set, and assigns this results set to suitable ABAP data objects.
The additions mainquery_clauses define which data can be read from the database in which form. The language element UNION can be used to combine the results sets of multiple queries. In this case, special rules query_clauses apply for specifying clauses. Finally, the following properties are defined:
In the following cases, the statement SELECT opens a loop that must be closed using ENDSELECT.
In each loop iteration, the SELECT statement assigns a row or a packet of rows to the data objects specified in target. If the last row has been assigned or the results set is empty, SELECT jumps to ENDSELECT. A database cursor is opened implicitly to process a SELECT loop, and is closed again when the loop has ended. In a single program, a maximum of 17 database cursors can be open simultaneously across the ABAP SQL interface. If more than 17 database cursors are opened, the runtime error DBSQL_TOO_MANY_OPEN_CURSOR occurs. A SELECT loop can be exited using the statements in the section Exiting Loops. If the total results set is passed to the data object in a single step, a loop is not opened and the statement ENDSELECT cannot be specified.
The INTO clause introduced using INTO|APPENDING must be specified as the final clause of the SELECT
statement and the optional additions UP
TO, OFFSET, and
abap_options must be specified after the INTO clause.
System Fields
The statement SELECT sets the values of the system fields sy-subrc and sy-dbcnt.
sy-subrc | Meaning |
0 | In each value passing to an ABAP data object, the statement SELECT sets sy-subrc to 0. In addition, SELECT sets sy-subrc to 0 before a SELECT loop is exited using ENDSELECT, if at least one row is passed in the loop. |
4 | The statement SELECT sets sy-subrc to 4 if the results set is empty. This means that no data is found on the database in most cases. Special rules apply when only aggregate expressions specified as columns are used in the SELECT list of the SELECT clause. |
8 | The statement SELECT sets sy-subrc to 8 if the addition FOR UPDATE is used in result, and the primary key is not fully specified after WHERE. |
After each value that is passed to an ABAP data object, the statement SELECT
sets sy-dbcnt to the number of rows passed. If an overflow occurs because
the number or rows is greater than 2,147,483,647, sy-dbcnt is set to -1.
If the results set is empty, sy-dbcnt is set to 0. As with sy-subrc, special rules apply if only
aggregate expressions
specified in columns are used in the
SELECT list of the SELECT clause.
Notes
Example
The example shows two SELECT statements that differ only in the arrangement of their SELECT and FROM clauses. The result of the two statements, which access two database tables via an INNER JOIN, is identical.
DATA cityfrom TYPE spfli-cityfrom VALUE 'NEW YORK'.
SELECT c~carrname, p~connid, p~cityfrom, p~cityto
FROM scarr AS c
INNER JOIN spfli AS p
ON c~carrid = p~carrid
WHERE p~cityfrom = @cityfrom
ORDER BY c~carrname, p~connid, p~cityfrom, p~cityto
INTO TABLE @DATA(result1)
UP TO 10 ROWS.
SELECT FROM scarr AS c
INNER JOIN spfli AS p
ON c~carrid = p~carrid
FIELDS c~carrname, p~connid, p~cityfrom, p~cityto
WHERE p~cityfrom = @cityfrom
ORDER BY c~carrname, p~connid, p~cityfrom, p~cityto
INTO TABLE @DATA(result2)
UP TO 10 ROWS.
ASSERT result2 = result1.
cl_demo_output=>display( result1 ).