INTO CLAUSE

Variants:




1. ... INTO wa

2. ... INTO CORRESPONDING FIELDS OF wa

3. ... INTO (f1, ..., fn)

4. ... INTO TABLE itab

5. ... INTO CORRESPONDING FIELDS OF TABLE itab

6. ... APPENDING TABLE itab

7. ... APPENDING CORRESPONDING FIELDS OF TABLE itab

Effect

With the statement SELECT or FETCH this statement determines the target area into which the data is to be read. If no data is read, the target area remains unchanged.

The result set is transported to the target area field by field. This means that the ABAP Dictionary data types of the result fields must correspond to the ABAP data types of the target fields as follows:

Result field Target field
Dict. data type ABAP data type
ACCP C or N
CHAR C, STRING
CLNT C, STRING
CUKY C, STRING
CURR I, P or F
DEC I, P or F
DATS D
FLTP F
INT1 I, P or F
INT2 I, P or F
INT4 I, P or F
LCHR C
LRAW X
LANG C, STRING
NUMC C or N
PREC I, P or F
QUAN I, P or F
RAW X, XSTRING
RAWSTRING X, XSTRING
SSTRING C, STRING
STRING C, STRING
TIMS T
UNIT C, STRING
VARC C, STRING

If the ABAP data type of the target field is C or X, the contents of the result field are placed left-justified in the target field. If the target field is too short, the result value is truncated on the right.

If the ABAP data type of the target field is N, the contents of the result field are placed right-justified in the target field and filled with zeros if required. If the target field is too short, the result value is truncated on the left.

If the ABAP data type of the target field is numeric, the target field must be long enough to hold the contents of the result field.

If a field in the result set contains a NULL value, the initial value of the ABAP data type corresponding to the field type is placed in the target area (see TABLES).

Depending on the database system, any violation of the correspondence rules can lead to a runtime error.

Note

In a SELECT ... ENDSELECT loop, the target area is readdressed in every iteration. If the target area is changed during the loop processing (for example by converting a field symbol), this is taken into account.

Variant 1

... INTO wa


Effect

Places the result set in the target area wa line by line. The way how the result set is transported to the target area is determined by the SELECT clause:

  1. SELECT * ... INTO wa
    The selected data is placed in wa according to the line structure of the database table dbtab. Therefore, the structure of wa does not have to correspond to the structure of the result set. However, to access the columns of the results line symbolically, it is essential that dbtab is contained as the left substructure in the structure of wa. The work area wa must always be at least as wide as the table work area dbtab. If wa is wider, the contents of the excess components of wa remain unchanged.
    If the database table dbtab or the target structure wa contains strings, wa must be compatible with the line structure of dbtab.

  2. SELECT f1 ... fn ... INTO wa
    The selected data is placed in wa according to the structure of the work area wa. The fields of the result set are transported into the components of wa from left to right. If wa has fewer components than the SELECT list, a runtime error occurs. If it has more, the contents of the excess components of wa remain unchanged.


In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs.See Open SQL and Unicode.

If the result of a selection is a table, the data is retrieved in a processing loop introduced by SELECT and concluded by ENDSELECT. The processing passes through the loop once for each line read. If the result is a single record, the closing ENDSELECT is omitted.

Examples

Output a list of all airlines (with short description and name):

DATA: wa TYPE scarr.

SELECT * INTO WA FROM scarr.
  WRITE: / wa-carrid, wa-carrname.
ENDSELECT.

Output a list of all airlines (with short description and name):

DATA: BEGIN OF wa1,
        client   TYPE scarr-mandt,
        carrid   TYPE scarr-carrid,
        carrname TYPE scarr-carrname,
        url      TYPE scarr-url,
        rest(100),
      END OF wa1.

SELECT * INTO wa1 FROM SCARR.
  WRITE: / wa1-carrid, wa1-carrname.
ENDSELECT.

Output a list of all airlines (with short description and name):

DATA: BEGIN OF wa2,
        carrid   TYPE scarr-carrid,
        carrname TYPE scarr-carrname,
      END OF wa2.
     END   OF WA2.
SELECT carrid carrname
       INTO wa2
       FROM scarr.
  WRITE: / wa2-carrid, wa2-carrname.
ENDSELECT.

Variant 2

... INTO CORRESPONDING FIELDS OF wa


Effect

Places the result set in the target area wa line by line. Each field of the result set is transported to the field of the same name in wa. If no such field exists, this is ignored, and no runtime error occurs.

If the result of a selection is a table, the data is retrieved in a processing loop introduced by SELECT and concluded by ENDSELECT. The processing passes through the loop once for each line read. If the result is a single record, the closing ENDSELECT is omitted.

Example

Output a list of all airlines (with short description and name):

DATA: carrier TYPE scarr.
DATA: tabname(10).
SELECT CARRID CARRNAME
tabname = 'SCARR'.
       FROM SCARR.
SELECT *
       INTO CORRESPONDING FIELDS OF carrier
       FROM (tabname).
  WRITE: / carrier-carrid, carrier-carrname.
ENDSELECT.

Notes

  1. Fields in the database table (or the join) are mapped to the identically-named fields in the target area at runtime. For runtime-critical applications, you should use variant 3 wherever possible, in case the fields to be selected or the database table are static.


  2. If a column name occurs more than once in a join, the contents of the last field with that name are placed in the identically-named field in the target area.


Variant 3

... INTO (f1, ..., fn)


Places the result set in the target area (f1, ..., fn). The fields of the result set are transported to the target fields fi from left to right. INTO (f1, ..., fn) is allowed only if a list with n elements is also specified in the SELECT clause.

If the result of a selection is a table, the data is retrieved in a processing loop introduced by SELECT and concluded by ENDSELECT. The processing passes through the loop once for each line read. If the result is a single record, the closing ENDSELECT is omitted.

Example

Output a list of all airlines (with short description and name):

DATA:  carrid   TYPE scarr-carrid,
       carrname TYPE scarr-carrname.
       CARRNAME LIKE SCARR-CARRNAME,
SELECT carrid carrname
       INTO (carrid, carrname)
       FROM scarr.
  WRITE: / carrid, carrname.
ENDSELECT.

Variant 4

... INTO TABLE itab


Addition:


... PACKAGE SIZE n

Effect

Works like ... INTO wa, except that the selected data is not placed in the internal table itab line by line, but in one single operation. In this case, SELECT does not introduce a processing loop, so there can be no ENDSELECT statement. The old contents of itab are overwritten. Fields of the internal table itab which are not filled are initialized based on their ABAP data type (see DATA).

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs.See Open SQL and Unicode.

Example

Output a list of all airlines (with short description and name):

DATA:  ITAB TYPE STANDARD TABLE OF scarr WITH NON-UNIQUE
                 DEFAULT KEY INITIAL SIZE 100,
       wa_itab TYPE scarr.
       WA_ITAB TYPE SCARR.
SELECT * INTO TABLE itab FROM scarr.
LOOP AT itab INTO wa_itab.
  WRITE: / wa_itab-carrid, wa_itab-carrname.
ENDLOOP.

Addition

... PACKAGE SIZE n

Effect

Works like ... INTO wa, except that the selected data is not placed in the internal table itab line by line, but in packets of n lines. The old contents of itab are overwritten.

If the result of the selection is a table, the data is retrieved in a processing loop introduced by SELECT and concluded by ENDSELECT. The processing passes through the loop once for each line read.

Example

Output a list of all airlines (with short description and name):

DATA: itab TYPE STANDARD TABLE OF SCARR WITH NON-UNIQUE
                 DEFAULT KEY INITIAL SIZE 10.
FIELD-SYMBOLS: <FS> TYPE scarr.

SELECT * INTO TABLE itab PACKAGE SIZE 20 FROM scarr.
  LOOP AT itab ASSIGNING <FS>.
    WRITE: / <FS>-carrid, <FS>-carrname.
  ENDLOOP.
ENDSELECT.

Variant 5

... INTO CORRESPONDING FIELDS OF TABLE itab


Addition:


... PACKAGE SIZE n

Effect

Works like ... INTO CORRESPONDING FIELDS OF wa, except that the selected data is not placed in the internal table itab line by line, but in a single operation. In this case, SELECT does not introduce a processing loop, so there can be no ENDSELECT statement. The old contents of itab are overwritten. Fields of the internal table itab which are not filled are initialized based on their ABAP data type (see DATA).

Addition

... PACKAGE SIZE n

Effect

Works as with ... INTO TABLE itab.

Variant 6

... APPENDING TABLE itab


Addition:


... PACKAGE SIZE n

Effect

Works like ... INTO TABLE itab, except that the read lines are appended to the old contents of the internal table itab.

Addition

... PACKAGE SIZE n

Effect

Works as with ... INTO TABLE itab.

Variant 7

... APPENDING CORRESPONDING FIELDS OF TABLE itab


Addition:

... PACKAGE SIZE n

Effect

Works like ... INTO CORRESPONDING FIELDS OF TABLE itab, except that the read lines are appended to the old contents of the internal table itab.

Addition

... PACKAGE SIZE n

Effect

Works as with ... INTO TABLE itab.

Note

If cursor processing is introduced with OPEN CURSOR, the sequence of FETCH statements may contain different INTO clauses - within a strict context. The following rules apply:

  1. Variants 1, 4 and 6 can be combined with each other if the types of the target areas wa or the line types of the internal tables itab are identical.
  2. Variants 2, 5 and 7 can be combined with each other if the types of the target areas wa or the line types of the internal tables itab are identical.
  3. Variant 3 cannot be combined with any other variant.


Notes

Performance:

  1. Whether it is better to read data into an internal table or into a work area, depends on the type of further processing: if the data is needed only once in a program, you should read it line by line into a work area using a SELECT-ENDSELECT loop. Reading the data into an internal table requires more storage and the processing speed is not increased considerably. If the data is needed more than once in a program, however, you should read it into an internal table. The disadvantage of the increased storage requirements is made up for by the advantage of a single selection.


  2. If the data is to be read into an internal table it is more cost-effective to read it in one single operation than to read it line by line into a work area and then appending it to the internal table using APPEND.


  3. The variants ... INTO CORRESPONDING FIELDS OF wa, ... INTO CORRESPONDING FIELDS OF TABLE itab, and ... APPENDING CORRESPONDING FIELDS OF TABLE itab require a slightly increased runtime - which is, however, independent of the size of the solution set - as compared with the corresponding variants without CORRESPONDING FIELDS. You should therefore avoid using them.

Additional help

Entering a Target Area