Reading Data into an Internal Table

You can write the result set of a line selection from a database table into an internal table in a single operation.

To do this, specify the internal table in the INTO clause of the SELECT statement as shown below:

Syntax

SELECT..... INTO TABLE <itab>.

In this case, SELECT does not start a loop, and no ENDSELECT statement is allowed.

If the internal table <itab> is not empty, its contents are overwritten with the data read by the SELECT statement.

TABLES SPFLI.

DATA ITAB LIKE SPFLI OCCURS 10 WITH HEADER LINE.

SELECT * FROM SPFLI INTO TABLE ITAB
WHERE CARRID = 'LH'.

LOOP AT ITAB.
   WRITE: / ITAB-CONNID, ITAB-CARRID.
ENDLOOP.

In this example, all lines from the database table SPFLI in which CARRID field contains "LH" are read into the internal table ITAB, where they can be processed further.

When specifying an internal table as the target area for selected lines, you can process the lines in packages or append lines to internal tables instead of overwriting the table contents. These options are described below.

Processing Lines in Packages

If you want to read the selected lines into an internal table in packages of predefined size, use the PACKAGE SIZE option of the INTO clause as follows:

Syntax

SELECT *..... INTO TABLE <itab> PACKAGE SIZE <n>.....

This statement opens a loop. You must terminate this loop with the ENDSELECT statement. For every package of <n> lines read, the system passes through the loop once. If <n> is less than or equal to 0, a runtime error occurs.

Outside the SELECT loop, the contents of the internal table are undetermined. If, therefore, you want to process the selected lines further, you must program the corresponding ABAP statements within the loop.

TABLES SPFLI.

DATA ITAB LIKE SPFLI OCCURS 5 WITH HEADER LINE.

SELECT * FROM SPFLI INTO TABLE ITAB PACKAGE SIZE 5
WHERE CARRID = 'LH'.

   LOOP AT ITAB.
      WRITE: / ITAB-CARRID, ITAB-CONNID.
   ENDLOOP.

   SKIP 1.

ENDSELECT.

In this example, all lines from the database table SPFLI in which CARRID field contains "LH" are read into the internal table ITAB in packages of 5 lines. Inside the SELECT loop, there is a second loop which writes these packages to the output list. The output list appears as follows:

Appending Lines to Internal Tables

To prevent the contents of an internal table from being overwritten, you can append the selected lines to the table instead. To do this, use the APPENDING clause instead of the INTO clause, as shown below:

Syntax

SELECT..... APPENDING TABLE <itab>.....

The only difference between this and the INTO clause described above is that lines are appended to the internal table <itab> instead of their contents. You can also use the PACKAGE SIZE option in this statement.