ABAP Keyword Documentation → ABAP - Short Reference
SELECT - Short Reference
Other versions: 7.31 | 7.40 | 7.54
Syntax
SELECT { { SINGLE [FOR UPDATE] }
| { [DISTINCT] { } } }
{ *
| { {col1|aggregate( [DISTINCT] col1 )} [AS a1]
{col2|aggregate( [DISTINCT] col2 )} [AS a2] ... }
| (column_syntax) }
FROM { { {dbtab [AS tabalias]}
| { [(] {dbtab_left [AS tabalias_left]} | join
{[INNER] JOIN}|{LEFT [OUTER] JOIN}
{dbtab_right [AS tabalias_right] ON join_cond} [)]}
| (dbtab_syntax) [AS tabalias] }
[CLIENT SPECIFIED]
[UP TO n ROWS]
[BYPASSING BUFFER]
[CONNECTION {con|(con_syntax)}] }
{ { INTO {[CORRESPONDING FIELDS OF] wa}|(dobj1, dobj2, ...) }
| { INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE itab
[PACKAGE SIZE n] } }
[ CREATING { READER|LOCATOR FOR { COLUMNS col1 col2 ... }
| { ALL [OTHER] [BLOB|CLOB] COLUMNS }
[READER|LOCATOR FOR ...] }
| { (crea_syntax) } ]
[[FOR ALL ENTRIES IN itab] WHERE sql_cond]
[GROUP BY {col1 col2 ...}|(column_syntax)]
[HAVING sql_cond]
[ORDER BY { {PRIMARY KEY}
|{{{col1|a1} [ASCENDING|DESCENDING]}
{{col2|a2} [ASCENDING|DESCENDING]}
... }
| (column_syntax) }].
...
[ENDSELECT.].
Effect
This statement reads data from one or more database tables into data objects. If the result set is not
assigned in one step, a loop is opened which is closed using ENDSELECT
. This loop provides the results in the target fields.
Additions
Specification of the rows
-
SINGLE|{ }
- Defines a single row or multiple row result set. -
FOR UPDATE
- Sets an exclusive lock in the database. -
DISTINCT
- Excludes duplicate rows from the result set.
Specification of the columns
-
*
- Reads all columns. -
col1 ... col2 ...
- Reads individual columnscol1
,col2
, ... -
aggregate( [DISTINCT] col1 ) ... aggregate( [DISTINCT] col2 ) ...
- Evaluates aggregate functionsaggregate
for columnscol1
,col2
.... Duplicate arguments can be excluded. -
AS a1 ... AS a2 ...
- Defines alternative column namesa1
,a2
, ... -
(column_syntax)
- Specifies the columns as the content ofcolumn_syntax
.
Specification of the database table(s)
-
FROM dbtab [AS tabalias]
Specifies a database tabledbtab
. -
FROM [(] {dbtab_left [AS tabalias_left]} | join
{[INNER] JOIN}|{LEFT [OUTER] JOIN}
{dbtab_right [AS tabalias_right] ON join_cond} [)]
Joins a database tabledbtab_left
or a join expressionjoin
with a database tabledbtab_right
to a join expression. You can create inner and outer joins with join conditionsjoin_cond
. -
(dbtab__syntax)
Specifies the database tables as the content ofsource_syntax
. -
AS tabalias
Defines an alternative table nametabalias
. -
CLIENT SPECIFIED
Deactivates automatic client handling. -
UP TO n ROWS
Restricts the number of rows read ton
. -
BYPASSING BUFFER
Bypasses SAP buffering. -
CONNECTION {con|(con_syntax)}
Executes the statement on a secondary database connection.
Specification of the target area
-
INTO wa|(dobj1, dobj2 ...)
Specifies a work areawa
or a list of data objectsdobj1 dobj2 ...
as the target fields. -
INTO|APPENDING TABLE itab [PACKAGE SIZE n]
Specifies an internal tableitab
as the target area. The result set is read into or attached to the table as a whole or in packages. -
CORRESPONDING FIELDS OF
Transports only the columns that have target fields with the same name. -
CREATING READER|LOCATOR FOR ... COLUMNS ...
Specifies what kind of LOB handle is created.
Specification of a condition
-
FOR ALL ENTRIES IN itab
Enables you to use a logical condition for all rows in an internal tableitab
afterWHERE
. -
WHERE sql_cond
Restricts the result set using the conditionsql_cond
. The condition can consist of different logical expressions.
Specification of a grouping
-
GROUP BY { {col1 col2 ...} | (column_syntax) }
Integrates groups of rows that have the same content in the columnscol1
,col2
, ... into one row of the result, where you can specify the columns dynamically incolumn_syntax
. -
HAVING sql_cond
Restricts rows combined in groups in the result set using a conditionsql_cond
.
Specification of a sorting
-
ORDER BY
{PRIMARY KEY}|{{{col1|a1} [ASCENDING|DESCENDING]}
{{col2|a2} [ASCENDING|DESCENDING]} ...}|(column_syntax)
Sorts the result set by primary key, statically specified columnscol1
,col2
, ... ora1
,a2
, ... or dynamically specified colums incolumn_syntax
. The sort direction can be specified for individual columns.