ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Reads → SELECT
SELECT - mainquery_clauses
Other versions:
7.31 | 7.40 | 7.54
Syntax
... [SINGLE [FOR UPDATE]]
{ FROM source
FIELDS select_clause }
| { select_clause
FROM source }
[[FOR ALL ENTRIES IN itab]
WHERE sql_cond]
[GROUP BY group] [
HAVING group_cond]
[ORDER BY sort_key]
[db_hints] ...
Effect
Possible clauses and additions of a
main query represented by
a standalone SELECT
statement. The clauses and additions define the results set of the SELECT
statement:
-
Rows of the results set
- Multirow Results Set
IfSINGLE
is not specified, and if aggregate expressions are not exclusively specified in theSELECT
list of theSELECT
clause select_clause, the results set is multirow or tabular by default. All database rows that are selected by the remaining additions of theSELECT
statement are included in the results set. If the addition ORDER BY is not used, the order of the rows in the results set is not defined and, if the same statementSELECT
is executed multiple times, the order may be different each time. A data object specified afterINTO
can be an internal table and the additionAPPENDING
can be used. If no internal table is specified afterINTO
orAPPENDING
, the statementSELECT
in multirow results sets triggers a loop that must be closed usingENDSELECT
.
- Single Row Results Set
The optional addition SINGLE defines a single-row results set that can be protected against parallel changes by another program. When usingSINGLE
, it is not possible to specify an internal table as target object, and the additionSINGLE
cannot be combined with all additions of theSELECT
statement.
-
Columns of the Results Set
SELECT
clause select_clause
defines the structure of the results set. It consists mainly of a
SELECT list that defines the columns of the results set. Duplicate rows can be excluded (this is optional).
-
Data Sources
FROM
clause specifies which
data sources source
are read. These can either be data sources in the database accessed by the current query or they can be
internal tables. Data sources in the database must be defined in ABAP Dictionary.
-
Conditions
WHERE
clause. The optional addition FOR
ALL ENTRIES can be used to compare the content of a column in the database with a component with all rows of a structured internal table itab
.
-
Grouping
GROUP BY
clause merges
multiple database rows into one row of the results set. The HAVING
clause restricts the merged rows.
-
Database Notes
db_hints
can be used to specify database hints.
-
Sorting
ORDER BY
clause sorts
the results set. If ORDER BY
is not specified, the order of the rows in the results set is undefined.
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
.
Notes
-
Although the
WHERE
condition is optional, for performance reasons, it should always be specified, and the results set should not be restricted in AS ABAP. -
The arrangement of the
FROM
clause before aSELECT
clause withFIELDS
supports tools such as code completion in the ABAP Editor.
- If a query is used to access a
CDS entity associated with a
CDS role and for which
CDS access control
is not disabled using the value #NOT_ALLOWED for the annotation @AccessControl.authorizationCheck or using the addition
WITH PRIVILEGED ACCESS
in theFROM
clause, only that data is read implicitly that matches the access condition defined in the CDS role. If data cannot be read, ABAP programs cannot distinguish whether this is due to the conditions of theSELECT
statement, the conditions of the CDS entity, or an associated CDS role. If the CDS database view database view of a CDS view is accessed, no access control takes place.
-
A
SELECT
clause started withFIELDS
after theFROM
clause leads to the strict mode as of Release 7.50.
Example
SELECT
statement with all possible clauses.
SELECT FROM sflight
FIELDS carrid,
connid,
SUM( seatsocc ) AS seatsocc
WHERE carrid = 'LH'
GROUP BY carrid, connid
HAVING SUM( seatsocc ) > 1000
ORDER BY carrid, connid
INTO TABLE @DATA(result).