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

Access internal table 1 line at a time

Select records from internal table using LOOP ENDLOOP instead of SELECT ENDSELECT. Put any ABAP logic inbetween loop statement to process the records you have selected. With header line, record gets transferred one record at a time from body of internal table into header line to work with the record. LOOP AT without INTO pertains to header line internal tables.

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.

LOOP AT itab01.
  WRITE: / itab01-surname, itab01-forename.
ENDLOOP.

LOOP AT itab02.
  IF itab02-surname = 'JONES'.
     itab02-surname = 'SMITH'.
     MODIFY itab02.
  ENDIF.
ENDLOOP.

***********************************************************

*New Way of defining Internal Tables
*Declare a Line Type
TYPES: BEGIN OF line01_typ,
         surname LIKE zemployees-surname,
         dob     LIKE zemployees-dob,
       END OF line01_typ.

*Declare the 'Table Type' based on the Line Type.
TYPES itab02_typ TYPE STANDARD TABLE OF line01_typ.

*Declare the table based on the 'Table Type'
DATA itab02 TYPE itab02_typ.

*Declare the Work Area to use with our Internal Table
DATA wa_itab02 TYPE line01_typ.

LOOP AT itab01 INTO wa_itab02.
  WRITE wa_itab02-surname.
ENDLOOP.

LOOP AT itab02 INTO wa_itab02.
  IF wa_itab02-surname = 'JONES'.
     wa_itab02-surname = 'SMITH'.
     MODIFY itab01 FROM wa_itab02.
  ENDIF.
ENDLOOP.