Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  READ TABLE itab 

READ TABLE - index

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... INDEX idx [USING KEY keyname] ... . 

Addition

... USING KEY keyname

Effect

If the INDEX addition is used, the READ statement reads the row of the row number specified in idx with respect to a table index. idx is a numerical expression position of the operand type i. If the value of idx is less than or equal to 0 or greater than the number of table rows, no row is read and sy-subrc is set to 4. If the read is successful, the system field sy-tabix contains the row number specified in idx in the primary or secondary table index used.

If the addition USING KEY is not used, the addition INDEX can only be specified for index tables and determines the row to be read from the primary table index.


Example

Reads the first ten rows of the internal table sflight_tab using the primary table index. Instead of the DO loop, the LOOP loop can normally also be used to do this.

DATA: sflight_tab TYPE SORTED TABLE OF sflight 
                  WITH NON-UNIQUE KEY seatsocc, 
      sflight_wa  LIKE LINE OF sflight_tab. 

... 

SELECT * 
       FROM sflight 
       INTO TABLE sflight_tab 
       WHERE carrid = 'LH' AND 
             connid = '400'. 

... 

DO 10 TIMES. 
  READ TABLE sflight_tab INDEX sy-index INTO sflight_wa. 
  IF sy-subrc <> 0. 
    EXIT. 
  ENDIF. 
  ... 
ENDDO. 

Addition

... USING KEY keyname

Effect

If the addition USING KEY is used, a table key can be declared in keyname to declare the table index to be used explicitly.

If the table has a sorted secondary key, this can be specified in keyname. The row to be read is then determined from its secondary table index. You cannot declare a secondary hashed key.

If the primary table key is specified under the name primary_key, the table must be an index table, and the behavior is the same as when USING KEY is not specified.


Note

If a sorted secondary key exists, the INDEX addition can be used for all table types, if USING KEY is used.


Example

Reads the first ten rows of the internal table sflight_tab using a secondary table index. Instead of the DO loop, the LOOP loop can also be used to do this.

DATA: sflight_tab TYPE HASHED TABLE OF sflight 
                  WITH UNIQUE KEY primary_key 
                       COMPONENTS carrid connid fldate 
                  WITH NON-UNIQUE SORTED KEY occupied_seats 
                       COMPONENTS seatsocc, 
      sflight_wa  LIKE LINE OF sflight_tab. 

... 

SELECT * 
       FROM sflight 
       INTO TABLE sflight_tab 
       WHERE carrid = 'LH' AND 
             connid = '400'. 

... 

DO 10 TIMES. 
  READ TABLE sflight_tab 
       INDEX sy-index USING KEY occupied_seats 
       INTO sflight_wa. 
  IF sy-subrc <> 0. 
    EXIT. 
  ENDIF. 
  ... 
ENDDO.