ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → READ TABLE itab
READ TABLE - index
Other versions: 7.31 | 7.40 | 7.54
Syntax
... INDEX idx [USING KEY keyname] ...
Addition
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.
Note
Table expressions
enable reads to be performed in operand positions too. Here an index is specified as a numeric argument idx
.
Example
Reads the first ten rows of the internal table sflight_tab
using the primary
table index. Instead of the DO
loop, a
LOOP or a corresponding iteration expression with FOR
is used for this purpose.
DATA: sflight_tab TYPE SORTED TABLE OF sflight
WITH NON-UNIQUE KEY seatsocc,
sflight_wa LIKE LINE OF sflight_tab.
...
SELECT *
FROM sflight
WHERE carrid = 'LH' AND
connid = '400'
INTO TABLE @sflight_tab.
...
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 specified in
keyname
to specify 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. A secondary
hash key cannot be specified.
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.
Notes
-
If a sorted secondary key exists, the addition
INDEX
can be used for all table categories, ifUSING KEY
is used. -
Table expressions
enable reads to be performed in operand positions too. The table key for an index is specified using
KEY keyname INDEX
.
Example
Reads the first ten rows of the internal table sflight_tab
using a secondary
table index. Instead of the DO
loop, a
LOOP or a corresponding iteration expression with FOR
is used for this purpose.
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
WHERE carrid = 'LH' AND
connid = '400'
INTO TABLE @sflight_tab.
...
DO 10 TIMES.
READ TABLE sflight_tab
INDEX sy-index USING KEY occupied_seats
INTO sflight_wa.
IF sy-subrc <> 0.
EXIT.
ENDIF.
...
ENDDO.