ABAP Keyword Documentation →  ABAP − Reference →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Reads →  SELECT clauses →  SELECT - INTO, APPENDING → 

SELECT - INTO target

Quick Reference

Syntax

... {     @dobj       }
  | {     @DATA(dobj) }
  | { NEW @dref       }
  | { NEW @DATA(dref) } ...


Alternatives:

1. ... @dobj

2. ... @DATA(dobj)

3. ... NEW @dref

4. ... NEW @DATA(dref)

Effect

Specifies a target area of the INTO clause of an ABAP SQL query. The possible target areas of the INTO clause are as follows:

Each target area can be specified as follows:

The addition NEW is used to create an anonymous data object to which data from the results set is written and to which a data reference variable dref points. The data reference variable is one of the following:

All alternatives can be 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.

Note

In the case of the variant NEW @DATA(dref), the SELECT list, the FROM clause, and any indicators can be specified dynamically. This is the only way of combining a dynamically defined results set with inline declarations. The same applies to inline declarations after FETCH.

Alternative 1

... @dobj


Effect

Specifies a previously declared host variable @dobj as a target area of the INTO clause. The data in the results set is written directly to the host variable dobj. Depending on the operand position, the host variable can be one of the following:

Writes to the host variable are made as described in the INTO clause.

Example

Uses different existing host variables as target areas of SELECT statements.

DATA:
  carrid   TYPE scarr-carrid,
  carrname TYPE scarr-carrname,
  url      TYPE scarr-url,
  carrier  TYPE scarr,
  carriers TYPE SORTED TABLE OF scarr WITH UNIQUE KEY carrid.

SELECT SINGLE carrid, carrname, url
       FROM scarr
       WHERE carrid ='UA'
       INTO (@carrid, @carrname, @url).

SELECT SINGLE *
       FROM scarr
       WHERE carrid ='LH'
       INTO @carrier.

SELECT *
       FROM scarr
       INTO TABLE @carriers.

Alternative 2

... @DATA(dobj)


Effect

Specifies a host variable @dobj declared inline as a target area of the INTO clause. The data in the results set is written directly to the host variable dobj. The inline declaration is made using the declaration operator DATA, which must be prefixed with the escape character @ here. Depending on the operand position, the host variable is declared as follows:

The prerequisites for an online declaration are as follows:

Notes

Example

Reads individual columns of a results set into various 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 an appropriate row type.

SELECT SINGLE
       FROM scarr
       FIELDS carrname,
              carrid,
              url
       WHERE carrid = 'LH'
       INTO (@DATA(carrname),@DATA(carrid),@DATA(url)).

SELECT SINGLE
       FROM scarr
       FIELDS carrname,
              carrid,
              url
       WHERE carrid = 'LH'
       INTO @DATA(wa).

SELECT FROM scarr
       FIELDS carrname,
              carrid,
              url
       INTO TABLE @DATA(itab).

ASSERT wa-carrname = carrname.
ASSERT wa-carrid   = carrid.
ASSERT wa-url      = url.

cl_demo_output=>new(
  )->write( wa
  )->write( itab )->display( ).

Example

In this example, all columns of a results set are read into an inner join in an internal table, the row type of which is declared as a nested structure with the same structure as the results set. The first component of the nested structure is called SCARR and includes all columns of this database table. The second component of the nested structure is called SPFLI and includes all columns of this 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.

TYPES BEGIN OF output_wa.
INCLUDE TYPE scarr AS scarr RENAMING WITH SUFFIX _scarr.
INCLUDE TYPE spfli AS spfli RENAMING WITH SUFFIX _spfli.
TYPES END OF output_wa.
TYPES output TYPE SORTED TABLE OF output_wa
             WITH NON-UNIQUE KEY carrid_scarr connid_spfli.

SELECT *
       FROM scarr
         INNER JOIN spfli ON scarr~carrid = spfli~carrid
       INTO TABLE @DATA(itab).

cl_demo_output=>display( CONV output( itab ) ).

Executable Example

Inline Declarations

Alternative 3

... NEW @dref


Effect

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 results set is written to the new anonymous data object. The data reference variable dref can be typed in full or generically.

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.
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 results set defined in the SELECT list must have unique names.

Unlike in inline declarations with @DATA(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 possible, the type check is made as part of the syntax check. If not, it is made 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:

Notes

Example

Uses a generically typed and a fully 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 and hence demonstrates approximately how the NEW addition works.

TYPES scarr_tab TYPE STANDARD TABLE OF scarr WITH EMPTY KEY.

DATA dref_data TYPE REF TO data.
SELECT FROM scarr
       FIELDS *
       INTO TABLE NEW @dref_data.
ASSIGN dref_data->* TO FIELD-SYMBOL(<fs>).

DATA dref_scarr_tab TYPE REF TO scarr_tab.
SELECT FROM scarr
       FIELDS *
       INTO TABLE NEW @dref_scarr_tab.

ASSERT <fs> = dref_scarr_tab->*.

cl_demo_output=>display( <fs> ).

dref_scarr_tab = NEW #( ).
SELECT FROM scarr
       FIELDS *
       INTO TABLE @dref_scarr_tab->*.
ASSERT <fs> = dref_scarr_tab->*.

Example

In this example, three anonymous data object of the type string are created. The columns of the results set are converted to string.

DATA:
  dref1 TYPE REF TO string,
  dref2 TYPE REF TO string,
  dref3 TYPE REF TO string.

SELECT SINGLE carrid, carrname, url
       FROM scarr
       WHERE carrid = 'UA'
       INTO (NEW @dref1, NEW @dref2, NEW @dref3).

cl_demo_output=>display( |{ dref1->* }, { dref2->* }, { dref3->* }| ).

Example

This example compares an INTO 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. If individual components are not accessed, the exact data type does not need to be known in the program.

SELECT *
       FROM scarr
         INNER JOIN spfli ON scarr~carrid = spfli~carrid
       INTO TABLE @DATA(itab).

DATA dref TYPE REF TO data.
SELECT *
     FROM scarr
       INNER JOIN spfli ON scarr~carrid = spfli~carrid
     INTO TABLE NEW @dref.
ASSIGN dref->* TO FIELD-SYMBOL(<fs>).

ASSERT <fs> =  itab.

Example

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.

DATA(cols) = `*`.
DATA(from) = `scarr INNER JOIN spfli ON scarr~carrid = spfli~carrid`.

OPEN CURSOR @DATA(dbcur) FOR
SELECT (cols)
     FROM (from).

DATA dref TYPE REF TO data.
FETCH NEXT CURSOR @dbcur
      INTO NEW @dref.
CLOSE CURSOR @dbcur.

ASSIGN dref->* TO FIELD-SYMBOL(<fs>).

Example

The program DEMO_SELECT_INTO_NEW_VARIANTS demonstrates how the NEW addition is used in different variants of the INTO clause.

Alternative 4

... NEW @DATA(dref)


Effect

The addition NEW in front of an inline declaration with the declaration operator @DATA 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:

The columns of the results set defined in the SELECT list must have unique names. If multiple FETCH statements access a database cursor opened usingOPEN 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 appropriate operand position.

Note

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

Example

Creates 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 ABAP Dictionary. dref_data,, on the other hand is typed generically with data after FETCH. This demonstrated by using RTTI methods.

SELECT *
       FROM scarr
       INTO TABLE NEW @DATA(dref_scarr).

OPEN CURSOR @DATA(dbcur) FOR
  SELECT *
         FROM scarr.
FETCH NEXT CURSOR @dbcur
     INTO TABLE NEW @DATA(dref_data).
CLOSE CURSOR @dbcur.

ASSIGN dref_data->* TO FIELD-SYMBOL(<fs>).
ASSERT dref_scarr->* = <fs>.

cl_demo_output=>new(

  )->write( |dref_scarr: {
            CAST cl_abap_tabledescr(
            CAST cl_abap_refdescr(
               cl_abap_typedescr=>describe_by_data( dref_scarr )
               )->get_referenced_type(
               ) )->get_table_line_type(
               )->absolute_name } |
  )->write( |dref_data:  {
            CAST cl_abap_refdescr(
              cl_abap_typedescr=>describe_by_data( dref_data )
              )->get_referenced_type(
              )->absolute_name }|

  )->display( dref_scarr->* ).

Executable Examples



Continue
Example SELECT, Inline Declarations
Example SELECT, Create Structure as Target Area
Example SELECT, Create Internal Table as Target Area