MichaelB.
7/25/2018 - 7:22 PM

Fill internal table with a header line

Use SELECT statement to fill internal table. INTO CORRESPONDING FIELDS OF TABLE means select all fields from the data dictionary table and move them into the fields of the internal table that match. It uses an array fetch: picks up the records and puts them in the table all at once -> much faster than using a loop. Does not use a header line, but uses a block by block array fetch to fill internal table. You can write out individual fields that you want to fetch. SELECT ENDSELECT is a loop, the corresponding fields form is an array fetch. MOVE statement moves data from one field to another. APPEND will tell system to take values from the header record and add them into the body of the internal table. Header record will still contain previous values after one loop if not cleared. INCLUDE statement simply copies the structure you declared before. Using INCLUDE STRUCTURE can save additional coding. Can be enhanced further by including multiple table structures with one internal table. However, make sure your field names are unique.

TABLES zemployees.

*Internal Table with Header line
DATA: BEGIN OF itab01 OCCURS 0,
        employee LIKE zemployees-employee,
        surname  LIKE zemployees-surname,
        forename LIKE zemployees-forename,
        title    LIKE zemployees-title,
        dob      LIKE zemployees-dob,
        los      TYPE i VALUE 3,
      END OF itab01.

*SELECT * FROM zemployees
*  INTO CORRESPONDING FIELDS OF TABLE itab01.

*SELECT surname forename FROM zemployees INTO CORRESPONDING FIELDS OF TABLE itab01.

SELECT * FROM zemployees.
  MOVE zemployees-employee TO itab01-employee.
  MOVE zemployees-surname  TO itab01-surname.
  MOVE zemployees-forename TO itab01-forename.
  MOVE zemployees-dob      TO itab01-dob.

  APPEND itab01.
ENDSELECT.

WRITE itab01-surname.

*****************************************************
* Alternative way of defining an internal table with a header line

*Internal Table with Header line
DATA: BEGIN OF itab02 OCCURS 0.
    INCLUDE STRUCTURE itab01.
DATA  END OF itab02.

*Internal Table with Header line
DATA: BEGIN OF itab03 OCCURS 0.
    INCLUDE STRUCTURE zemployees.
DATA  END OF itab03.

*Internal Table with Header line
DATA: BEGIN OF itab04 OCCURS 0.
    INCLUDE STRUCTURE zemployees.
    INCLUDE STRUCTURE itab01.
    DATA ee_count TYPE i.
DATA  END OF itab04.