SAP SELECT INTO TARGET ABAP Statements
Get Example source ABAP code based on a different SAP table
• DATA SELECT INTO
• FINAL SELECT INTO
• NEW SELECT INTO
SELECT>, INTO target> Short Reference >
ABAP_SYNTAX
... ${ @dobj $}
$| ${ @DATA(dobj)$|@FINAL(dobj) $}
$| ${ NEW @dref $}
$| ${ NEW @DATA(dref)$|@FINAL(dref) $} ...>
ABAP_ALTERNATIVES:
1 ... @dobj>
2 ... @DATA(dobj)$|@FINAL(dobj)>
3 ... NEW @dref>
4 ... NEW @DATA(dref)$|@FINAL(dref)>
What does it do?
Specifies a target area of the INTO>> clause of an AB_SQL query. The possible target areas of the INTO>> clause are as follows:
Elementary data objects elem1>, elem2>, ... in a comma-separated list.
Individual work areas wa>.
Internal tables itab> after TABLE>.
Each target area can be specified as follows:
If the addition NEW> is not used, dobj> specifies the data object directly to which the data from the result set is written. The target area is one of the following:
A previously declared host variable> @dobj>
A host variable> dobj> declared inline> using @DATA>
The addition NEW> is used to create an anonymous data object> to which data from the result set is written and to which a data reference variable> dref> points. The data reference variable is one of the following:
A previously declared host variable> @dref>
A host variable> dref> declared inline> using @DATA>
All alternatives can occur combined in a comma-separated list for elementary data objects as target areas. One of these alternatives must be selected when a single work area or an internal table is specified.
Latest notes:
In the case of the variant NEW @DATA(dref)$|@FINAL(dref)>, the SELECT> list>, the FROM> clause>, and any indicators> can be specified dynamically. This is the only way of combining a dynamically defined result set with inline declarations.
NON_V5_HINTS
The same applies to inline declarations after FETCH >>.
ABAP_HINT_END
ABAP Alternative 1 ... @dobj>
What does it do?
Specifies a previously declared host variable> @dobj> as a target area of the INTO>> clause. The data in the result set is written directly to the host variable dobj>. Depending on the operand position, the host variable can be one of the following:
In the comma-separated list elem1>, elem2>, ..., an elementary data object> that matches the type of the associated column of the result set or in other words: The type of the associated column of the result set can be converted to the data type of the data object.
As a single work area wa>, a data object that meets the prerequisites>.
As an internal table, any table category whose row structure meets the prerequisites>.
Writes to the host variable are made as described in the INTO>> clause.
ABAP_EXAMPLE_VX5
Use of different existing host variables as target areas of SELECT > statements.
ABEXA 00616
ABAP_EXAMPLE_END
• /atDATA SELECT INTO
• /atFINAL SELECT INTO
ABAP Alternative 2 ... @DATA(dobj)$|@FINAL(dobj)>
What does it do?
Specifies a host variable> @dobj > declared inline as the target area of the INTO>> clause. The data in the result set is written directly to the host variable dobj>. The inline declaration is made with one of the declaration operators> DATA>> or FINAL >>, which must be prefixed with the escape character @> here. Depending on the operand position, the host variable is declared as follows:
In the comma-separated list elem1>, elem2>, ..., an elementary variable is declared. The data type of the variables is constructed as follows from the associated column of the result set:
The ABAP type to which the dictionary type of a column> of a data source is assigned> is used for this column.
The ABAP type to which the result type of an SQL expression> is assigned> is used for this expression.
The ABAP type of a host variable> is used directly for this variable specified as a single SQL expression.
Work area and internal table:
For INTO @DATA(wa)> or INTO @FINAL(wa)>, a flat data object wa> is declared as a single work area.
For INTO TABLE @DATA(itab)> or INTO TABLE @FINAL(itab)>, a standard table> itab> with an empty table key> is declared as an internal table. The data type of wa> or the row type of the internal table is constructed as follows in accordance with the structure of the result set defined after SELECT> and the number of data sources specified after FROM>>:
If the result set in the SELECT> list> is defined using a single specified column col_spec>> for which no name can be identified, the data type of wa> or the row type of itab> is its elementary type.
If the result set in the SELECT> list> is defined using a single specified column col_spec>> for which no name can be identified, the data type of wa> or the row type of itab> is a structure with a component, with its elementary type.
If the result set in the SELECT> list> is defined using a single data_source~*> or a list of multiple specified columns col_spec>>, the data type of wa> or the row type of itab> is a structure with elementary components. The data types of the components are the elementary types of the columns in the result set in the order defined there.
If data_source~*> is specified in the SELECT> list> together with other elements, the data type of wa> or the row type of itab> is a nested structure. For each data source data_source>> specified in this way, a substructure is generated with the name or alias name of the table or view. The data types of the components of the substructures are the elementary types of the data source in the order defined there. If the data source is a common table expression> declared using WITH>>, the first character +> of its name is ignored for the name of the substructure.
If the result set in the SELECT> list> is defined using *>, the data type depends on the number of data sources> specified after FROM>>:
In reads from a single data source> data_source>, the data type of wa> or the row type of itab> is the same as in a definition of the result set using a single data_source~*> (see above), that is, a structure with elementary components.
In reads from multiple data sources> data_source1>, data_source2>, ... using a join>, the data type of wa> or the row type of itab> is the same as in a definition of the result set using data_source1~*, data_source2~*, ...>. (see above), that is, a structure with a substructure for each data source.
The names of the elementary components of a structure match the names of the associated columns from the result set>. Any alias names defined there are respected.
As with an elementary data object in a comma-separated list, the elementary data type of an elementary data object or of an elementary component of a structure is constructed from the type of the associated column of the result set (see above).
If the addition INDICATORS>> is used, a substructure called null_ind> is added at the end of the structure or row structure declared inline. For each column in the result set, this substructure contains an identically named component of type x> and length 1 in the same order. If preceding components of the structure declared inline are substructures, the substructure null_ind> is also structured accordingly.
The prerequisites for an inline declaration are as follows:
The result set can be specified dynamically only when combined with the addition NEW>. If the addition NEW> is not specified, the structure of the result set must be known statically. The SELECT> list> and any indicators> must be specified statically.
BEGIN_SECTION VERSION 5 OUT
After FETCH>>, an inline declaration can only be made together with the addition NEW>.
END_SECTION VERSION 5 OUT
The result set defined in the SELECT> list> cannot have multiple columns with the same name. This can be bypassed using alias names.
In a result set> with multiple rows or if the addition INDICATORS>> is specified, every SQL expression> and every aggregate expression> must have an alias name.
Alias names of the SELECT> list> must comply with the naming conventions> for internal program names. More specifically, they cannot contain a minus sign ->.
BEGIN_SECTION VERSION 5 OUT
If the obsolete addition CLIENT SPECIFIED>> is used to access a client-dependent CDS entity>, a name must be specified for the client column at the same time.
END_SECTION VERSION 5 OUT
The addition CORRESPONDING FIELDS OF>> cannot be used.
Latest notes:
Column specifications for which no name can be identified are SQL expressions and aggregate expressions without alias name.
When an inline declaration @DATA(itab)> or @FINAL(itab)> is specified after APPENDING TABLE>, this addition usually works like INTO TABLE > and is therefore pointless in this combination.
BEGIN_SECTION SAP_INTERNAL_HINT
ABAP_EXCEPTION You can prefill itab> dynamically before SELECT>.
END_SECTION SAP_INTERNAL_HINT
Currently, only standard tables with an empty key and without secondary keys can be declared inline as internal tables in the INTO> clause of a SELECT> statement.
NON_V5_HINTS
Therefore, this kind of inline declaration is only suitable> for tables, which are mainly accessed by an index. If key accesses are important, the internal table should not be declared inline but with a declaration statement and with an appropriate key.
When inline declarations are used, the syntax check is performed in a ABAP_STRICT_740_SP08 strict mode / , which handles the statement more strictly than the regular syntax check.
ABAP_HINT_END
ABAP_EXAMPLE_VX5
Reading of individual columns of a result set into different target areas declared inline. carrname>, carrid>, and url> are elementary data objects. wa> is a structure with elementary components. itab> is a standard table with the corresponding row type.
ABEXA 00617
ABAP_EXAMPLE_END
ABAP_EXAMPLE_VX5
Reading of all columns of a result set into an inner join in an internal table whose row type is declared as a nested structure with the same structure as the result set. The first component of the nested structure is called SCARR> and includes all columns of this DDIC database table. The second component of the nested structure is called SPFLI > and includes all columns of this DDIC database table. The content of the columns MANDT> and CARRID> in both tables is redundant. For the output, the internal table with a nested row type is converted to an output table without substructures.
ABEXA 00618
ABAP_EXAMPLE_END
ABAP_EXAMPLE_ABEXA
Inline Declarations>
ABAP_EXAMPLE_END
ABAP Alternative 3 ... NEW @dref>
What does it do?
The addition NEW> creates an anonymous data object> as the target area of the INTO> clause>. dref > expects a previously declared data reference variable> that points to the data object after the object is created. The data of the result set is written to the new anonymous data object. The data reference variable dref> can be typed completely or generically.
If the data reference variable dref> is typed completely, its static type> must follow the same rules as for a directly specified data object @dobj>:
In a comma-separated list (..., dref, ...)>, it must be an elementary data type to which the type of the associated column of the result set can be converted.
In a single work area wa>, the type must meet the prerequisites> for work areas.
In an internal table, it must be a table type with any table category whose row structure meets the prerequisites>. The anonymous data object is created with the type of the data reference variable. The static type> of the data reference variable matches the dynamic type >. Writes to the anonymous data object are made as described in the INTO>> clause.
If the data reference variable dref> is typed generically with data>>, the data type of the anonymous data object or the dynamic type > of dref> is constructed depending on the operand position in exactly the same way as the data type of dobj> in the inline declaration