ABAP - Keyword Documentation →  ABAP - Reference →  Processing External Data →  ABAP Database Accesses →  Open SQL →  Open SQL - Reads →  SELECT clauses → 

SELECT - INTO, APPENDING

Quick Reference

Syntax

... { INTO
      { {[CORRESPONDING FIELDS OF] @wa}|(@dobj1, @dobj2, ...)} }
  | { INTO|APPENDING
         [CORRESPONDING FIELDS OF] TABLE @itab [PACKAGE SIZE n]}
  | { INTO @DATA(wa) }
  | { INTO TABLE @DATA(itab) [PACKAGE SIZE n] }
    [ EXTENDED RESULT @oref ]
    [ creating ] ...  .

Alternatives:

1. ... INTO [CORRESPONDING FIELDS OF] @wa

2. ... INTO (@dobj1, @dobj2, ... )

3. ... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE @itab [PACKAGE SIZE n]

4. ... INTO @DATA(wa)

5. ... INTO TABLE @DATA(itab) [PACKAGE SIZE n]

Effect

The information after INTO or APPENDING specifies to which data objects the results set of a SELECT statement, WITH statement or a FETCH statement is assigned. The following:

can be specified as a host variable with escape character @ The declaration operator DATA can be used to perform appropriate inline declarations.

When field symbols or dereferenced reference variables are specified for the work area, individual data objects, or internal tables in a SELECT loop closed using ENDSELECT, the data object that is the target of a field symbol or reference variable is identified precisely 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 EXTENDED RESULT addition can be used to provide an extended result in a result object.

If a LOB of the results set is associated with a LOB handle, it may be necessary to use creating to specify whether a data stream or a locator is being used.

Notes

Alternative 1

... INTO [CORRESPONDING FIELDS OF] @wa


Effect

For wa, a data object can be specified as a host variable using the escape character @. This must meet certain prerequisites without specifying CORRESPONDING FIELDS OF. If the results set consists of a single row, this row is assigned to wa. If the results set has multiple rows, SELECT must be followed by an ENDSELECT statement; the results set is assigned to the work area wa row-by-row and can be evaluated in the loop. After ENDSELECT, 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.

The rows of the results set are assigned as follows, based on the SELECT list::

If all columns are read with *, CORRESPONDING FIELDS is not specified and the SELECT statement is not the main query of a WITH statement, then SELECT behaves as follows:
If the results 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 results set consists of multiple columns, it must be a structure and the following rules apply:

Assignment rules apply to the individual assignments. If a LOB of a reference variable for LOB handle is assigned, a LOB handle is created.

If CORRESPONDING FIELDS is used, the columns of the results set defined in the SELECT list correspond to the components of the target area. Among other things, there must be at least one match or there must be an identically named component in the target area for each explicitly specified column.

Notes

Example

In this example, four columns of the results set are read into four correspondingly named components of a work area.

DATA wa TYPE spfli.

SELECT carrid, connid, cityfrom, cityto
       FROM spfli
       INTO CORRESPONDING FIELDS OF @wa.
  cl_demo_output=>write_data( wa ).
ENDSELECT.
cl_demo_output=>display( ).

Alternative 2

... INTO (@dobj1, @dobj2, ... )


Effect

If the results set consists of multiple columns or aggregate expressions specified explicitly in the SELECT list, a list of elementary data objects dobj1, dobj2, ... (in parentheses and separated by commas) can be specified after INTO. The following can be specified as a single data object:

The same number of elementary data objects dobj must be specified as there are columns in the results set. The content of the columns in the results sets is assigned to the data objects from left to right, according to the order specified after SELECT. Assignment rules apply to the individual assignments. If a LOB of a reference variable for LOB handle is assigned, a LOB handle is created. If the results set is empty, the data objects remain unchanged.

If the results set consists of one row, the columns are assigned from that row. If the results set contains multiple rows, SELECT must be followed by an ENDSELECT statement; the columns of the results set are assigned to the data objects row-by-row and they can be evaluated in a loop. If used in the statement FETCH, the columns of the row are extracted at the current cursor position.

Note

No list can be specified after INTO if the results set is defined by specifying * or dbtab1~*, dbtab2~*, ... in the SELECT list.

Example

In this example, four columns of the results set are read into four individually specified columns of a structure. Unlike in the previous example, the runtime environment does not compare names here.

DATA wa TYPE spfli.

SELECT carrid, connid, cityfrom, cityto
       FROM spfli
       INTO (@wa-carrid,
             @wa-connid,
             @wa-cityfrom,
             @wa-cityto).
  cl_demo_output=>write_data( wa ).
ENDSELECT.
cl_demo_output=>display( ).

Example

Gets the same columns as in the preceding example with inline declarations for the target objects.

SELECT carrid, connid, cityfrom, cityto
       FROM spfli
       INTO (@DATA(carrid),
             @DATA(connid),
             @DATA(cityfrom),
             @DATA(cityto)).
  cl_demo_output=>write_data(
   |{ carrid } { connid } { cityfrom } { cityto }| ).
ENDSELECT.
cl_demo_output=>display( ).

Alternative 3

... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE @itab [PACKAGE SIZE n]


Addition:

... PACKAGE SIZE n

Effect

If the results set consists of multiple rows, an internal table itab of any table type can be specified as a host variable using the escape character @ after INTO or APPENDING. The row type of the internal table must meet the prerequisites.

The results set 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 INTO is used, the internal table is initialized. Previous rows remain intact if APPENDING is used. If there is a conflict with an existing unique table key, a non-handleable exception is raised like in the case of INSERT LINES OF.

Before any assignment of a row of the results sets, an initial row of the internal table itab is created and the row of the results set is assigned to this row. When assigning a row of the results 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 used, all rows of the results set are inserted in the internal table itab and the ENDSELECT statement must not be specified after SELECT.

If the results set is empty, the internal table is initialized when INTO is used, and remains unchanged when APPENDING is used.

Notes

Addition

... PACKAGE SIZE n

Effect

If the addition PACKAGE SIZE is specified, all rows of the results set for SELECT are processed in a loop, which must be closed with ENDSELECT. They are inserted in packages of n rows in the internal table itab. n expects a host variable with a prefixed escape character @, a host expression, or a literal of type i (that contains the number of rows). When n is specified as a data object, it should be prefixed by the escape character @. The content of n must match the data type i in accordance with the rules for a lossless assignment. In the strict mode of the syntax check in Release 7.51, n must have type b, s, i or int8. 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 results set are inserted in 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, 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.

Notes

Example

In this example, all columns of a results set are read into an internal table, the row type of which is a nested structure with the same structure as the results set. Note that in practice, the column carrid exists twice in the results set with the same content and, after the assignment, this content is stored redundantly in the columns struc1-carrid and struc2-carrid of the internal table.

DATA: BEGIN OF wa,
        struc1 TYPE scarr,
        struc2 TYPE spfli,
      END OF wa.

DATA itab LIKE SORTED TABLE OF wa
          WITH UNIQUE KEY table_line.

DATA: BEGIN OF output_wa,
        carrid LIKE wa-struc1-carrid,
        carrname LIKE wa-struc1-carrname,
        connid LIKE wa-struc2-connid,
      END OF output_wa,
      output LIKE TABLE OF output_wa WITH EMPTY KEY.

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

LOOP AT itab INTO wa.
  output_wa-carrid = wa-struc1-carrid.
  output_wa-carrname = wa-struc1-carrname.
  output_wa-connid = wa-struc2-connid.
  APPEND output_wa TO output.
ENDLOOP.

cl_demo_output=>display_data( output ).

Alternative 4

... INTO @DATA(wa)


Alternative 5

... INTO TABLE @DATA(itab) [PACKAGE SIZE n]


Effect

Inline declaration of the full target area. The declaration operator DATA must be prefixed with the escape character @. The data type of the new data object is constructed in accordance with the structure of the results set defined after SELECT and the number of data sources specified after FROM. INTO @DATA(wa) declares a flat data object wa of this type; INTO TABLE @DATA(itab) declares a standard table itab of this row type with an empty table key. The same applies to PACKAGE SIZE as when specifying an existing internal table.

The prerequisites for an online declaration are as follows:

The data type of the declared data object wa or itab is determined as follows:

The elementary data type of an elementary data object or an elementary component of a structure is constructed as follows:

The names of the elementary components of a structure match the names of the associated columns from the results set. Any alias names defined there are respected.

Notes



Continue
SELECT - EXTENDED RESULT
SELECT - CREATING
SELECT - Assignment Rules
SELECT - LOB Handles