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 @DATA$|FINAL(dobj) described above. This means the following:
In a comma-separated list (..., dref, ...), an elementary data object is created whose type is determined by the type of the associated column of the result set.
For a single work area in INTO NEW @dref, a flat data object is created. The type of the new data object is constructed in the same way as in an inline declaration using @DATA$|FINAL(dobj).
For INTO TABLE NEW @dref, a standard table with an empty table key is created as an internal table. The row type of the internal table is constructed in the same way as in an inline declaration using @DATA$|FINAL(dobj). The anonymous data object is created with this type. The static type of the data reference variable is more general than the dynamic type. Writes to the anonymous data object are made as described in the INTO clause. The columns of the result set defined in the SELECT list must have unique names.
Unlike in inline declarations with @DATA|FINAL(dobj), the type of the anonymous data object can also be created at runtime. This means that the addition NEW can also be specified in the following cases:
If the structure of the result set cannot be known statically due to the use of dynamic tokens.
BEGIN_SECTION VERSION 5 OUT
After FETCH.
END_SECTION VERSION 5 OUT
If possible, the type check takes place as part of the syntax check and otherwise at runtime. If an error is not detected until runtime, an exception of the class CX_SY_DYNAMIC_OSQL_SEMANTICS is raised.
The following restrictions apply:
The addition NEW can only be specified after INTO and not after APPENDING.
BEGIN_SECTION VERSION 5 OUT
If multiple FETCH statements access a database cursor opened using OPEN CURSOR, a data reference variable used after NEW can be typed generically only if the first of these FETCH statements has the addition NEW with a generically typed data reference variable in the appropriate operand position.
END_SECTION VERSION 5 OUT



Latest notes:

The addition NEW works in a similar way as creating an anonymous data object with the instance operator NEW directly in front of the AB-SQL statement and using the dereferenced reference variable as a target area. The addition NEW has the advantage that the data type of the anonymous data object is constructed in a suitable way and this works especially for dynamic tokens as well.
NON_V5_HINTS
If the addition NEW is used, the syntax check is performed in ABAP_STRICT_777 strict mode from ABAP_RELEASE ABAP_777 / .
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Use of a generically typed and a completely typed data reference variable after NEW. The anonymous data objects created here both have the same type and the same content. The third SELECT statement writes to an anonymous data object created previously using the instance operator NEW demonstrating roughly how the NEW addition works.
ABEXA 00619
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
In this example, three anonymous data objects of the type string are created. The columns of the result set are converted to string .
ABEXA 00620
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
This example compares an INTO clause with an inline declaration of the target area (see above) with an INTO clause with the addition NEW. In both cases, the same data type is constructed, namely an internal table with a nested row structure. As long as individual components are not accessed, the exact data type does not need to be known in the program.
ABEXA 00621
ABAP_EXAMPLE_END
BEGIN_SECTION VERSION 5 OUT

ABAP_EXAMPLE_VX
As in the previous example, but after FETCH and with a dynamically specified SELECT list and the FROM clause after OPEN CURSOR. In this case, it is not possible to make a direct inline declaration of the target area in the INTO clause. It is, however, possible to use the addition NEW.
ABEXA 00622
ABAP_EXAMPLE_END
END_SECTION VERSION 5 OUT
VX_EXA_ONLY



Example ABAP Coding

The class CL_DEMO_SELECT_INTO_NEW demonstrates how the NEW addition is used in different variants of the INTO clause.
ABAP_EXAMPLE_END

ABAP Alternative 4 ... NEW @DATA(dref)$|@FINAL(dref)

What does it do?
The addition NEW in front of an inline declaration with the declaration operators @DATA or @FINAL works like the previous variant, however the data reference variable dref that points to the new anonymous data object is declared inline. The static type of the data reference variable dref is defined as follows:
If the data type of the new anonymous data object is known statically, dref is also typed with this type.
If the data type of the new anonymous data object cannot be known until runtime, dref is typed with the generic type data.
BEGIN_SECTION VERSION 5 OUT This is the case when the structure of the result set is not known statically due to the use of dynamic tokens and in inline declarations after FETCH.
END_SECTION VERSION 5 OUT
The columns of the result set defined in the SELECT list must have unique names.
BEGIN_SECTION VERSION 5 OUT If multiple FETCH statements access a database cursor opened using OPEN CURSOR, NEW and an inline declaration can be used only if this is also the case in the first of these FETCH statements in the corresponding operand position.
END_SECTION VERSION 5 OUT



Latest notes:

The addition NEW can also be used to make an inline declaration together with dynamic tokens. The data reference variable that points to the target area created as an anonymous data object is declared, however, instead of the direct target area.
NON_V5_HINTS
The addition NEW can also be used to make an inline declaration after FETCH. The data reference variable that points to the target area created as an anonymous data object is declared, however, instead of the direct target area.
ABAP_HINT_END

ABAP_EXAMPLE_VX
Creation of anonymous data objects as target areas together with inline declarations of the data reference variables. After SELECT with static tokens, the data reference variable dref_scarr has the static type of an internal table with the row type SCARR from the ABAP Dictionary. dref_data, on the other hand is typed generically with data after FETCH. This is demonstrated using RTTI methods.
ABEXA 00623
ABAP_EXAMPLE_END

ABAP_EXAMPLE_V5
Creation of an anonymous data object as target area together with an inline declaration of the data reference variable. After SELECT with static tokens, the data reference variable dref_scarr has the static type of an internal table with the row type SCARR from the ABAP Dictionary. This is demonstrated using RTTI methods.
ABEXA 01694
ABAP_EXAMPLE_END

ABAP_EXAMPLES_ABEXA
Create Structure as Target Area
Create Internal Table as Target Area
ABAP_EXAMPLE_END

Return to menu