ABAP Keyword Documentation → ABAP - Reference → Processing External Data → ABAP - Database Accesses → Open SQL → Open SQL - Read Accesses
SELECT
Other versions: 7.31 | 7.40 | 7.54
Syntax
SELECT result
FROM source
INTO|APPENDING target
[[FOR ALL ENTRIES IN itab] WHERE sql_cond]
[GROUP BY
group] [HAVING group_cond]
[ORDER BY sort_key].
...
[ENDSELECT].
Effect
SELECT
is the Open SQL statement for reading data from one or more database tables into data objects.
The select statement reads a result set (whose structure is determined in
result
) from the database tables specified in
source
, and assigns the data from the result set to the data objects
specified in target
. You can
restrict the result set using the WHERE
addition. The addition GROUP BY
merges multiple database rows into one row of the result set. The addition
HAVING
restricts the merged rows. The addition ORDER BY
sorts the result set.
The data objects specified in target
must correspond to the result set
result. This means that the result set is either assigned to the data objects in one step, or by rows or packets of rows. The latter is the case:
-
If an assignment is made to a non-table-like target range (meaning a
SELECT
statement without the addition INTO|APPENDING ... TABLE), a loop closed byENDSELECT
always occurs, except in the following instances:
- The addition
SINGLE
for reading a single row is specified behindSELECT
.
- The columns of the results set are specified statically, they contain only
aggregate functions,
and the addition
GROUP BY
is not specified.
-
If an assignment is made to a table-like target range (meaning a
SELECT
statement with the addition INTO|APPENDING ... TABLE), a loop closed byENDSELECT
occurs whenever the additionPACKAGE SIZE
is used.
If these approproaches are used, the SELECT
statement opens a loop, which
must be closed with 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 result 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. You can exit the loop using the statements listed in the
Exiting Loops section. 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.
With the exception of the INTO
and APPENDING
additions,
the entries in the SELECT
statement specify which data should be read by the database and in what form. This requirement is implemented in the
database interface for the database system´s programming interface and is then passed to the database system. The data is read in
packets from the database and is transported from the database server to the current
application server.
On the application server, the data is passed to the data objects of the ABAP program in accordance
with the settings specified in the INTO
and APPENDING
additions.
System Fields
sy-subrc | Meaning |
---|---|
0 | The SELECT statement sets sy-subrc to 0 forevery value passed to an ABAP data object. The SELECT statement also setssy-subrc to 0 before it exits a SELECT loop with ENDSELECT if at least one row was passed. |
4 | The SELECT statement sets sy-subrc to 4 if the result set is empty, that is, if no data was found in the database. Special rules apply whenaggregate expressions only are used in result . |
8 | The SELECT statement sets sy-subrc to 8 if the FOR UPDATE addition is used in result , and theprimary key is not fully specified after WHERE . |
After each value that is passed to an ABAP data object, the SELECT
statement
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 result set is empty, sy-dbcnt
is set to 0. As with sy-subrc
, special rules apply if
aggregate expressions only are used in result
.
Notes
-
An obsolete short form is possible (apart from in
classes), for which you do not need to specify the target area with
INTO
orAPPENDING
if a single database table or a single view is specified statically afterFROM
, and a table work area dbtab is declared with theTABLES
statement for the corresponding database table or view. In this case, the system implicitly extends theSELECT
statement with the additionINTO dbtab
. -
Although the
WHERE
condition is optional, for performance reasons, you should always specify it, and the result set should not be restricted in the application layer. -
SELECT
loops can be nested. For performance reasons, you should check whether it would be more efficient to use a join or a subquery. -
Within a
SELECT
loop, you cannot execute any statements that lead to a database commit or database rollback, causing the corresponding database cursor to be closed as a result. -
If change accesses are performed on the database
tables read by a
SELECT
loop within the loop, the behavior is database-specific and undefined. Avoid this kind of access if possible. -
The statement
ENDSELECT
closes all the reader streams which are associated with theSELECT
loop.