Summary -

In this topic, we described about the below sections -

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'.