Entering content frame

Specifying a Target Area Locate the document in its SAP Library structure

The INTO clause defines the target area into which the selection from the SELECT clause is written. Suitable target areas are variables whose data type is compatible with or convertible into that of the selection in the SELECT clause.

The SELECT clause determines the data type of the target area as follows:

·        The linesspecification in the SELECT clause determines the depth of the target area, that is, whether it is a flat or a tabular structure.

·        The colsspecification in the SELECT clause determines the structure (line type) of the target area.

This graphic is explained in the accompanying text

If you select a single line, the target area must be flat. If you select more than one line, the target area may be either tabular or flat. If the target area is flat, you need to use a SELECT loop.

When you select all of the columns, the target area must either be a structure or convertible into one. When you select individual columns, the target area can be a component of a structure or a single field.

The elementary data types in the selection in the SELECT clause are Dictionary types. These Dictionary types must be able to be converted into the ABAP data types of the corresponding elementary components of the target area. For a table of data types, refer to Data Types in the ABAP Dictionary.

Specifying a Flat Work Area

To read data into one, use the following in the INTO clause:

SELECT ... INTO [CORRESPONDING FIELDS OF] wa ...

The target area wa must be at least as large as the line to be read into it. When you read the data into wa, its previous contents are overwritten. However, the components of wa that are not affected by the SELECTstatement retain their previous values.

If the work area is structured, you can use the CORRESPONDING FIELDS addition. This transfers only the contents of fields whose names are identical in the database table and the work area. This includes any alias column names that you specified in the selection.

Specifying Internal Tables

To do this, use the following in the INTO clause:

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

The internal table is filled with all of the lines of the selection. When you use INTO, all existing lines in the table are deleted. When you use APPENDING; the new lines are added to the existing internal table <itab>.

If you use the PACKAGE SIZE addition, the lines of the selection are not written into the internal table at once, but in packets. You can define packets of n lines that are written one after the other into the internal table.

Specifying Single Fields

If you specify single columns of the database table or aggregate expressions in the SELECT clause, you can read the data into single fields for a single entry or for multiple entries in a SELECT loop. To read data into single fields, use the following in the INTO clause:

SELECT ... INTO (f1, f2, ...) ...

You must specify as many individual fields f1,  f2, … as are specified in the field list of the SELECT clause. The fields in the SELECT clause are assigned, from left to right, to the fields in the list in the INTO clause.

Example

Flat structure as target area

DATA wa TYPE spfli.

SELECT *
  INTO wa
  FROM spfli.

  WRITE: / wa-carrid...

ENDSELECT.

This example uses a flat structure with the same data type as the database table SPFLI as the target area in a SELECT loop. Within the loop, it is possible to address the contents of the individual columns.

DATA spfli TYPE spfli.

SELECT  *
  FROM  spfli.

  WRITE: / spfli-carrid...

ENDSELECT.

This example declares a structure spfli with the same name as the database table you want to read. This structure is used implicitly as the target area in the SELECT loop. Since the names are the same, it is possible to overlook the fact that you are working with an ABAP data object here and not the database table itself.

Example

Internal table as target area

REPORT demo_select_into_table.

DATA: BEGIN OF wa,
         carrid   TYPE spfli-carrid,
         connid   TYPE spfli-connid,
         cityfrom TYPE spfli-cityfrom,
         cityto   TYPE spfli-cityto,
      END OF wa,
      itab LIKE SORTED TABLE OF wa
                WITH NON-UNIQUE KEY cityfrom cityto.

SELECT carrid connid cityfrom cityto
  INTO CORRESPONDING FIELDS OF TABLE itab
  FROM spfli.

IF sy-subrc EQ 0.
  WRITE: / sy-dbcnt, 'Connections'.

  SKIP.
  LOOP AT itab INTO wa.
    WRITE: / wa-carrid, wa-connid, wa-cityfrom, wa-cityto.
  ENDLOOP.
ENDIF.

The list output is:

This graphic is explained in the accompanying text

The example uses a sorted table itab as a target area. This table has four fields with the same name and data type as the database table SPFLI. The program uses the CORRESPONDING FIELDS addition to place the columns from the SELECT clause into the corresponding fields of the internal table. Because itab is a sorted table, the data is inserted into the table sorted by the table key of itab.

Example

Reading packets into an internal table

REPORT demo_select_into_package.

DATA: wa   TYPE spfli,
      itab TYPE SORTED TABLE OF spfli
                WITH UNIQUE KEY carrid connid.

SELECT carrid connid
  FROM spfli
  INTO CORRESPONDING FIELDS OF TABLE itab
       PACKAGE SIZE 3.

  LOOP AT itab INTO wa.
    WRITE: / wa-carrid, wa-connid.
  ENDLOOP.

  SKIP 1.

ENDSELECT.

The list output is:

This graphic is explained in the accompanying text

The example reads packets of three lines each into the sorted table itab. In each pass of the SELECT loop, the internal table has a different sorted content.

If you were to use APPENDING instead of INTO, the list would look like this:

This graphic is explained in the accompanying text

In each loop pass, a new packet is sorted into the internal table.

Example

Single fields as target area:

REPORT demo_select_single_fields.

DATA:   average TYPE p DECIMALS 2,
        sum     TYPE p DECIMALS 2.

SELECT AVG( luggweight ) SUM( luggweight )
  INTO (average, sum)
  FROM sbook.

WRITE: / 'Average:', average,
       / 'Sum    :', sum.

The list output is:

This graphic is explained in the accompanying text

The SELECT clause contains two aggregate expressions for calculating the average and sum of the field LUGGWEIGHT from database table SBOOK. The target fields are called average and sum.

Example

Using aliases:

REPORT demo_select_as.

DATA: BEGIN OF luggage,
        average TYPE p DECIMALS 2,
        sum     TYPE p DECIMALS 2,
      END OF luggage.

SELECT AVG( luggweight ) AS average SUM( luggweight ) AS sum
  INTO CORRESPONDING FIELDS OF luggage
  FROM sbook.

WRITE: / 'Average:', luggage-average,
       / 'Sum    :', luggage-sum.

The list output is:

This graphic is explained in the accompanying text

This example has the same effect as the previous one. The only difference is that a structure is used as the target area instead of individual fields, and that the names of the structure components are used as aliases in the SELECT clause.

 

 

 

Leaving content frame