Reading Values from ABAP Internal Table

Data can be read from an internal table using the following 2 statements.

  • READ TABLE Used to read single row
  • LOOP AT / ENDLOOP Used to read multiple rows

The syntax for READ TABLE is as follows.

READ TABLE [INTO Workarea] [INDEX |WITH KEY]

In READ TABLE statement we need to specify either INDEX or KEY but not both at the same time. If we want to read the third row from the internal table, then specify the index as 3 in the INDEX clause of the READ statement. Index is a relative row number of the internal table.

If the record is found then the found record is copied to work area and SY-SUBRC is set to 0. Otherwise SY-SUBRC is set to 4 and work area remains unchanged.

*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)    TYPE n,
       name(10) TYPE c,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it TYPE TABLE OF ty_student.

gwa_student-id    = 1.
gwa_student-name  = 'JOHN'.
APPEND gwa_student TO it.

gwa_student-id    = 2.
gwa_student-name  = 'JIM'.
APPEND gwa_student TO it.

gwa_student-id    = 3.
gwa_student-name  = 'JACK'.
APPEND gwa_student TO it.

READ TABLE it INTO gwa_student INDEX 3.
IF sy-subrc = 0.
  WRITE: gwa_student-id, gwa_student-name.
ELSE.
  WRITE 'No Record Found'.
ENDIF.

Output

We can also read a row from the internal table based on the value in a particular field using KEY clause of the READ statement. If more than one row found in the internal table with the same value specified in the KEY clause then only the first row will be retrieved.

Let us read a record from internal table where name is JIM.

READ TABLE it INTO gwa_student WITH KEY name = 'JIM'.
IF sy-subrc = 0.
  WRITE: gwa_student-id, gwa_student-name.
ELSE.
  WRITE 'No Record Found'.
ENDIF.

Output

The syntax for LOOP statement is as follows.

LOOP AT [INTO Workarea] [FROM] [TO] [WHERE expression]. 
ENDLOOP. 


The rows are read from the internal table one at a time and placed in the work area. The lines between LOOP and ENDLOOP are executed for each record read from the internal table. Once all the rows in the internal table are processed the control goes to the next line after the ENDLOOP statement.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_employee TYPE zemployee,
      gt_employee  TYPE TABLE OF zemployee.

*Fill the internal table with database values
SELECT * FROM zemployee INTO TABLE gt_employee.

WRITE:/ 'Display all the records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee.
  WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.

SKIP.

WRITE:/ 'Display only first 2 records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee TO 2.
  WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.

SKIP.

WRITE:/ 'Dont Display first 2 records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee FROM 3.
  WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.

SKIP.

WRITE:/ 'Display only 2nd & 3rd records in the internal table' COLOR 5.
LOOP AT gt_employee INTO gwa_employee FROM 2 TO 3.
  WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.

SKIP.

WRITE:/ 'Display records based on condition' COLOR 5.
WRITE:/ 'Display records where place is LONDON' COLOR 5.
LOOP AT gt_employee INTO gwa_employee WHERE place = 'LONDON'.
  WRITE:/ gwa_employee-id, gwa_employee-name, gwa_employee-place.
ENDLOOP.


Output


10 Comments

  1. hey i have question..what can be changed while lopping at internal table…like which value can be changed? (e.g. , sum, lead to) pls need this. Thank you.

Comments are closed.