AS ABAP Release 758, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Read Access → SELECT, clauses →
SELECT, INTO, APPENDING
Syntax
... { INTO
(
elem1, elem2, ...) }
| { INTO
[CORRESPONDING FIELDS OF]
wa [indicators] }
| { INTO|APPENDING
[CORRESPONDING FIELDS OF] TABLE
itab [indicators]
[PACKAGE SIZE n] }
[ extended_result ]
[ creating ] ... .
1. ... INTO (elem1, elem2, ...)
2. ... INTO [CORRESPONDING FIELDS OF] wa ...
3. ... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE itab ...
Effect
The additions INTO or APPENDING construct the INTO clause of a SELECT, WITH, or FETCH statement. They define which ABAP data objects are assigned the data of the result set of a query and how this assignment is made. The following options are available:
The data objects elem1, elem2, ...., wa, and itab can be specified as existing host variables or declared inline using @DATA or @FINAL. An addition NEW makes it possible to create target areas implicitly as anonymous data objects.
The additions have the following meaning:
These additions can all be used together with the following exceptions:
If the target area is specified using field symbols or reference variables, and a SELECT loop is closed using ENDSELECT, the data object referenced by a field symbol or reference variable is identified exactly once when the loop is entered. This data object is used as a target area in each loop pass. Any modifications to the assignment of a field symbol or reference variable within the loop are ignored.
The INTO clause must be the final clause of a main query and the additions UP TO, OFFSET, and abap_options must be placed after the INTO clause.
Hints
... INTO (elem1, elem2, ... )
Effect
Specifies a parenthesized and comma-separated list of elementary data objects elem1, elem2, ... as target areas of the INTO clause. This specification is only possible if the columns of the result set are defined using one or more explicitly specified columns col_spec in the SELECT list. Each of the data objects elem1, elem2, ... can be specified as existing host variables or declared inline using @DATA or @FINAL. The addition NEW allows the creation of anonymous data objects.
The comma-separated list must have the same number of elements as columns in the result set. The content of the columns in the result set is assigned to the data objects specified in the list from left to right in accordance with the order specified after SELECT. Assignment rules apply to the individual assignments. If an LOB is assigned to a reference variable for LOB handles, an LOB handle is created. If an offset/length is specified to access a data object, +off must not be omitted. If the result set is empty, the data objects remain unchanged. If a catchable exception is raised when the data objects are written to, their content is undefined when the exception is handled.
If the result set consists of one row, the columns of this row are assigned to the data objects. If the result set has multiple rows, the statements SELECT and WITH are used to open a SELECT loop that itself must be closed using ENDSELECT or ENDWITH. A SELECT loop assigns the columns of the result set to the data objects row by row and evaluates them. If used in the statement FETCH, the columns of the row are extracted at the current cursor position.
Hints
Example
Reading of the three columns of a result set to three elementary data objects. The first data object carrid is a previously declared host variable. The second data object carrname is a host variable declared inline. The third data object is an anonymous data object created using NEW and to which a data reference variable dref declared inline points.
DATA carrid TYPE scarr-carrid.
SELECT carrid, carrname, url
FROM scarr
ORDER BY carrid
INTO (@carrid,
@FINAL(carrname),
NEW @FINAL(dref)).
cl_demo_output=>write( |{ carrid
}, { carrname
}, { dref->* } | ).
ENDSELECT.
cl_demo_output=>display( ).
Example
Reading of the four columns of a result set into four individually specified components of a structure. Unlike when writing to a work area using CORRESPONDING FIELDS (see below), the runtime framework does not compare names here.
DATA:
BEGIN OF wa,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF wa.
SELECT SINGLE
FROM spfli
FIELDS carrid, connid, cityfrom, cityto
WHERE carrid = 'LH' AND connid = '400'
INTO (@wa-carrid,
@wa-connid,
@wa-cityfrom,
@wa-cityto).
cl_demo_output=>display( wa ).
... INTO [CORRESPONDING FIELDS OF] wa
Effect
Specifies a single work area wa as a target area of the INTO clause. This can be specified for all result sets. The data object wa can be specified as an existing host variable or declared inline using @DATA or @FINAL. The addition NEW allows the creation of an anonymous data object.
If the result set consists of a single row, this row is assigned to the work area wa. If the result set has multiple rows, the statements SELECT and WITH are used to open a SELECT loop that itself must be closed using ENDSELECT or ENDWITH. A SELECT loop assigns the result set to the work area wa row by row and evaluates it. After ENDSELECT or ENDWITH, the work area wa contains the row that was assigned last. If used in the FETCH statement, a row is extracted at the current cursor position. If the result is empty, the work area remains unchanged. If a catchable exception is raised when the work area is filled, its content is undefined when the exception is handled.
If the optional addition CORRESPONDING FIELDS OF is specified, wa must be a structure. This addition specifies that only the content of columns that have identically named components in wa is assigned to them. If the addition CORRESPONDING FIELDS OF is not specified, wa must meet the prerequisites described under Work Areas in Statements. The rows of the result set are assigned as follows, based on the definition of the result set in the SELECT list:
If all columns of the data sources are read using *, CORRESPONDING FIELDS is not specified and the SELECT statement is not the main query of a WITH statement, SELECT behaves as follows:
If the result set consists of a single column specified explicitly after SELECT or a single SQL expression or a single aggregate expression, wa can be an elementary data object or a structure. If the result set consists of multiple columns, it must be a structure and the following rules apply:
Assignment rules apply to the individual assignments. If an LOB of a reference variable for LOB handles is assigned, an LOB handle is created. If an offset/length is specified to access a substring of wa, +off must not be omitted.
If CORRESPONDING FIELDS is used, the columns of the result set defined in the SELECT list must match the components of the target area on a certain level. Among other things, there must be at least one name match or there must be an identically named component in the target area for each explicitly specified column.
Hints
Example
Reading of all columns of a row of a result set into structured work areas. In the first SELECT statement, wa is a previously declared host variable. In the second SELECT statement, wa_inl is a host variable declared inline. In the third SELECT statement, the work area is created as an anonymous data object using NEW. This object is pointed to using a data reference variable dref declared inline.
DATA wa TYPE scarr.
SELECT SINGLE *
FROM scarr
WHERE carrid = 'LH'
INTO @wa.
SELECT SINGLE *
FROM scarr
WHERE carrid = 'LH'
INTO @FINAL(wa_inl).
ASSERT wa_inl = wa.
SELECT SINGLE *
FROM scarr
WHERE carrid = 'LH'
INTO NEW @FINAL(dref).
ASSERT dref->* = wa.
cl_demo_output=>display( dref->* ).
Example
Reading of the four columns of a result set into four respective components of the same name in a work area in a different order.
DATA:
BEGIN OF wa,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF wa.
SELECT SINGLE
FROM spfli
FIELDS carrid, connid, cityfrom, cityto
WHERE carrid = 'LH' AND connid = '400'
INTO CORRESPONDING FIELDS OF @wa.
cl_demo_output=>display( wa ).
... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE itab
Effect
Specifies an internal table itab as a target area of the INTO clause. This can be specified for all result sets. The data object itab can be specified as an existing host variable or declared inline using @DATA or @FINAL. The addition NEW makes it possible to create an anonymous data object.
An internal table can only be specified after INTO TABLE or after APPENDING TABLE if the result set has multiple rows. The internal table can have any table category. Its row type must meet the same prerequisites as a work area wa.
The result set with multiple rows is inserted into the internal table itab row-by-row in accordance with the rules of INSERT. A sorting process is executed in the case of a sorted table.
If there is a conflict with an existing unique table key, an uncatchable exception is raised like in the case of the statement INSERT LINES OF.
Before any assignment of a row of the result set, an initial row of the internal table itab is created and the row of the result set is assigned to this row. When assigning a row of the result set to a row of the internal table with or without CORRESPONDING FIELDS, the same rules apply as when assigning to an individual work area wa (see above) with the exception that when inserting into internal tables, LOB handles can be created as locators but not as reader streams.
If the addition PACKAGE SIZE is not specified, all rows of the result set are inserted into the internal table itab and the statements ENDSELECT or ENDWITH statement must not be specified after SELECT or WITH.
If the result set is empty, the internal table is initialized when INTO is used, and remains unchanged when APPENDING is used. If a catchable exception is raised when the internal table is filled, its content is undefined when the exception is handled.
Hints
Example
Reading of all columns of all rows of a result set into structured internal tables. In the first SELECT statement, itab is a previously declared host variable. In the second SELECT statement, itab_inl is a host variable declared inline. In the third SELECT statement, the internal table is created as an anonymous data object using NEW. This object is pointed to using a data reference variable dref declared inline.
DATA itab TYPE STANDARD TABLE OF scarr
WITH EMPTY KEY.
SELECT *
FROM scarr
ORDER BY carrid
INTO TABLE @itab.
SELECT *
FROM scarr
ORDER BY carrid
INTO TABLE @FINAL(itab_inl).
ASSERT itab_inl = itab.
SELECT *
FROM scarr
ORDER BY carrid
INTO TABLE NEW @FINAL(dref).
ASSERT dref->* = itab.
cl_demo_output=>display( dref->* ).
Example
Reading of the four columns of a result set into four respective components of the same name in an internal table in a different order.
TYPES:
BEGIN OF wa,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF wa.
DATA itab TYPE SORTED TABLE OF wa WITH UNIQUE KEY table_line.
SELECT FROM spfli
FIELDS carrid, connid, cityfrom, cityto
INTO CORRESPONDING FIELDS OF TABLE @itab.
cl_demo_output=>display( itab ).
Example
The following example for APPENDING TABLE creates the same internal table as the example for UNION.
SELECT FROM scarr
FIELDS carrname,
CAST( '-' AS CHAR( 4 ) ) AS connid,
CAST( '-' AS CHAR( 20 ) ) AS cityfrom,
CAST( '-' AS CHAR( 20 ) ) AS cityto
WHERE carrid = 'LH'
INTO TABLE @DATA(result).
SELECT FROM spfli
FIELDS '-' AS carrname,
connid,
cityfrom,
cityto
WHERE carrid = 'LH'
APPENDING TABLE @result.
SORT result BY carrname DESCENDING connid cityfrom cityto.
cl_demo_output=>display( result ).
... PACKAGE SIZE n
Effect
If the addition PACKAGE SIZE is specified after INTO|APPENDING TABLE, the rows of the result set are inserted into the internal table itab in packages of n rows. In the case of the statements SELECT and WITH, a SELECT loop is opened that itself must be closed using ENDSELECT or ENDWITH. n expects a host variable, a host expression, or a literal of type i that contains the number of rows. If n is an untyped literal or a host variable, its content must match the data type i in accordance with the rules for a lossless assignment. n must have the type b, s, i, or int8. This is checked in strict mode of the syntax check from ABAP release 7.51. If the value of n is less than 0, an exception is raised that cannot be handled. If n is equal to 0, all rows of the result set are inserted into the internal table itab. If used in the statement FETCH, n rows are extracted from the current cursor position.
If INTO is used, the internal table is initialized before each insertion and, in the SELECT loop, it only contains the rows of the current package. If APPENDING is used, a further package is added to the existing rows of the internal table for each SELECT loop or for each extraction using FETCH.
After ENDSELECT or ENDWITH, the content of itab is not defined if INTO is used. That is, the table can either contain the rows of the last package or it can be initial. If APPENDING is used, the content of itab retains the state of the last loop pass.
Hints
Example
Reading and output of the columns of the DDIC database table SPFLI in packages of 10 rows.
FINAL(out) = cl_demo_output=>new( ).
SELECT carrid, connid, cityfrom, cityto
FROM spfli
ORDER BY carrid, connid, cityfrom, cityto
INTO TABLE @FINAL(result) PACKAGE SIZE 10.
out->next_section( |Package { sy-dbcnt / 10 }|
)->write( result ).
ENDSELECT.
out->display( ).