Summary -

In this topic, we described about the below sections -

Internal tables used for processing the data dynamically during the program execution. So, the data reading from the internal table can be done during the program execution. The internal table reading can be done in two ways -

  • READ Statement
  • LOOP Statement

READ Statement -

READ statement used to read the internal table. We can read the table lines by using the following simple READ TABLE statement syntax -

READ TABLE <itable> INTO <work-area>.

  • <work-area> - Specifies work area that is having the same structure as line of the internal table.
  • <itable> - Specifies the internal table.

We can read specific records from internal table by providing the table index number or key value. The key value provided for reading the line is called as Search Key. The READ statement to read the records by using the INDEX key syntax shown below -

READ TABLE <itable> INTO <work-area> 
		[INDEX <index-num>|WITH KEY <ifield-name> = <ifield-value>].

  • <work-area> - Represents a work area that is compatible with the line of the internal table.
  • <itable> - Specifies the internal table.
  • <index-num> - Specifies the table index number.
  • <ifield-name> - Specifies the table column name.
  • <ifield-value> - Specifies the column value.

When search key or index-num used to get the matching line, the processing starts from beginning of the table until the match found. SY-TABIX contains the index value of the line read.

SY-SUBRC is set to 0, if an entry with the specified index is found. If the specified index is less than 0, then run-time error occurs. If the specified index exceeds table size, then SY-SUBRC is set to 4.


COMPARING Clause -

READ statement is also used to retrieve the table data by comparing work area all fields using the COMPARING clause. The syntax for the same is shown below -

READ TABLE <itable> INTO <work-area> 
	[COMPARING { { f1 f2 ... fn }|{ALL FIELDS}|{NO FIELDS} }] 
	[TRANSPORTING { { f1 f2 ... fn }|{ALL FIELDS} }]

  • <work-area> - Represents a work area that is compatible with the line of the internal table.
  • <itable> - Specifies the internal table.
  • f1 f2 … fn - Specifies the table fields.

The COMPARING clause compares the specified fields (f1 f2 ... fn) from the retrived line with the corresponding fields of the work area. If ALL FIELDS is specified, all fields are compared. If no NO FIELDS is specified, no fields are compared. If the content of the compared fields are identical, sy-subrc is set to 0. If the content of the compared fields is not identical, sy-subrc set to 2.

If the TRANSPORTING is specified, the specified fields (f1 f2 ...fn) from the retrived line are assigned to the corresponding fields of the work area. If ALL FIELDS is specified, all the components are assigned. COMPARING must be specified before TRANSPORTING.


Example -

Simple example to read the data from product information internal table with below structure.

Pid -Character (10)
Pname - Character (40)
Pamount - Decimal (10)


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* internal table Structure creation
TYPES: BEGIN OF t_product,
       pid(10)     TYPE C,
       pname(40)   TYPE C,
       pamount(10) TYPE P,
       END OF t_product.

* Data & internal table declaration
DATA: wa TYPE t_product,
      it TYPE TABLE OF t_product,
      it1 TYPE TABLE OF t_product.

* inserting data to the internal table of INDEX 1
wa-pid     = 'IFB1'.
wa-pname   = 'IFB WASHING MACHINE'.
wa-pamount = 31000.
INSERT wa INTO TABLE it.

* inserting data to the internal table of INDEX 2
wa-pid     = 'IFB2'.
wa-pname   = 'IFB SPLIT AC'.
wa-pamount = 38000.
INSERT wa INTO TABLE it.

WRITE /'displaying IT table data INDEX 1 data...'.

* Reading internal table of index 1
READ TABLE it INTO wa INDEX 1.

IF sy-subrc = 0.
  WRITE :/ wa-pid, wa-pname, wa-pamount.
ELSE.
  WRITE 'No Record Found'.
ENDIF.

ULINE.

WRITE /'displaying IT table data with key IFB1...'.

* Reading internal table for all the records
READ TABLE it INTO wa WITH KEY pid = 'IFB1'.

IF sy-subrc = 0.
  WRITE :/ wa-pid, wa-pname, wa-pamount.
ELSE.
  WRITE 'No Record Found'.
ENDIF.
ULINE.

Output -

READ Statement Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

t_product - Structure declaration, wa - work area of type t_product and it - internal table declaration of type t_product.

INSERT wa INTO TABLE it - Inserting work area(wa) to the table(it), READ TABLE it INTO wa INDEX 1. - Reading 1st record from the internal table it.

READ TABLE it INTO wa WITH KEY pid = 'IFB1'. - Reading record which has the key pid = 'IFB1'.


LOOP Statement -

LOOP Statement reads line by line into <work-area> or into the table work area. LOOP statement is another statement used for reading or processing the internal table.

The processing statements-block should be coded in between LOOP and ENDLOOP. For each read, the statements-block coded in between LOOP and ENDLOOP processed one time.

LOOP AT <itable> [INTO <work-area>] 
	[FROM <n1>] [TO <n2>] [WHERE <condition>].
...
ENDLOOP.

  • <itable> - Specifies the internal table.
  • <work-area> - Specifies the table work area.
  • FROM <n1> - Specifies the index of the first line to be read.
  • TO <n2> - Specifies the index of the last line to be read.
  • WHERE <condition> - Specifies the logical expression and the operand in the logical expression must be a component of <itable>.

SY-TABIX contains the index of the current retrived line once the LOOP statement gets executed. The loop processing ends as soon as all lines of the table have been processed.

SY-SUBRC = 0: If at least one line retrieved. Otherwise it is set to 4.

The FROM and TO options restrict the number of lines and specifies the number of lines that the system must read. The WHERE option filters the lines that satisfies the condition. Without the WHERE option, the system read all lines. So, it is always best practise of specifying FROM, TO or WHERE options to improve performance.


Example -

Simple example to read the data using LOOP statement from product information internal table with below structure.

Pid -Character (10)
Pname - Character (40)
Pamount - Decimal (10)


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INTERNAL_TABLE.

* internal table Structure creation
TYPES: BEGIN OF t_product,
       pid(10)     TYPE C,
       pname(40)   TYPE C,
       pamount(10) TYPE P,
       END OF t_product.

* Data & internal table declaration
DATA: wa TYPE t_product,
      it TYPE TABLE OF t_product,
      it1 TYPE TABLE OF t_product.

* inserting data to the internal table of INDEX 1
wa-pid     = 'IFB1'.
wa-pname   = 'IFB WASHING MACHINE'.
wa-pamount = 31000.
INSERT wa INTO TABLE it.

* inserting data to the internal table of INDEX 2
wa-pid     = 'IFB2'.
wa-pname   = 'IFB SPLIT AC'.
wa-pamount = 38000.
INSERT wa INTO TABLE it.

WRITE 'displaying IT table data...'.
* Reading internal table for all the records
LOOP AT it INTO wa.
  IF sy-subrc = 0.
    WRITE :/ wa-pid, wa-pname, wa-pamount.
  ELSE.
    WRITE 'No Record Found'.
  ENDIF.
ENDLOOP.

Output -

LOOP Statement Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

t_product - Structure declaration, wa - work area of type t_product and it - internal table declaration of type t_product.

INSERT wa INTO TABLE it - Inserting work area(wa) to the table(it) and LOOP AT it INTO wa - Loop to read the internal table to work area record by record.