ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Read Accesses → 

SELECT

Quick Reference

Syntax

SELECT [SINGLE [FOR UPDATE]]
       { select_clause
         FROM source }
      |{ FROM source
         FIELDS select_clause }
       [[FOR ALL ENTRIES IN itab] WHERE sql_cond]
       [GROUP BY group] [HAVING group_cond]
       [UNION [ALL|DISTINCT]  select]
       [ORDER BY sort_key]
       INTO|APPENDING target
       [additional_options].
  ...
[ENDSELECT].

Effect

SELECT is the Open SQL statement for reading data from one or more database tables, classic views, or CDS entities into data objects.

The SELECT clause select_clause can be specified before or after the FROM clause. After the FROM clause, the SELECT clause must be prefixed with the addition FIELDS.

The INTO clause introduced using INTO|APPENDING should be specified as the final clause of the SELECT statement. In this case, the optional additions additional_options must be located after the INTO clause. For compatibility reasons, the INTO clause can be placed before or after the FROM clause. The additions additional_options can then be placed before or after the FROM clause. In the strict mode of the syntax check from Release 7.50, the INTO clause must be the last clause.

The data objects specified in target must match the results set result. The results set of the can be assigned to the data objects either

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 Open 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.

With the exception of the additions INTO and APPENDING, the information in the statement SELECT specifies which data should be read by the database and in what form. This requirement is implemented in the database interface for the programming interface of the database system 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 additions INTO and APPENDING.

System Fields

The statement SELECT sets the values of the system fields sy-subrc and sy-dbcnt.

sy-subrc Meaning
0 The statement SELECT sets sy-subrc to 0 for every value passed to an ABAP data object. The SELECT statement also sets sy-subrc to 0 before it exits a SELECT loop with ENDSELECT if at least one row was passed.
4 The statement SELECT sets sy-subrc to 4 if the results set is empty, that is, if no data was found in the database. 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
       INTO TABLE @DATA(result1).

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
       INTO TABLE @DATA(result2).

ASSERT result2 = result1.
cl_demo_output=>display( result1 ).



Continue
SELECT - SINGLE
SELECT - select_clause
SELECT - FROM
SELECT - WHERE
SELECT - GROUP BY
SELECT - HAVING
SELECT - UNION
SELECT - ORDER BY
SELECT - INTO
SELECT - additional_options
ENDSELECT
SELECT - Examples