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

READ TABLE - table_key

Short Reference

Syntax

... { FROM wa [USING KEY keyname] }
  | { WITH TABLE KEY [keyname COMPONENTS]
                     {comp_name1|(name1)} = operand1
                     {comp_name2|(name2)} = operand2
                     ...                             } ... .

Alternatives:

1. ... FROM wa [USING KEY keyname]

2. ... WITH TABLE KEY [keyname COMPONENTS] ...

Effect

Specifying a Table Key as a Search Key Either the primary table key or a secondary table key can be specified. The values can be declared either implicitly in a work area wa behind FROM or by listing the components of the table key explicitly behind TABLE KEY.

When the primary table key is used, the table categories are accessed as follows:

When the secondary table key is used, a binary scan is used in the sorted key case and a hash algorithm is used in the hash key case.

Note

Note that the sy-tabix system field is always set in relation to the used table key. If the value of sy-tabix is used as an index specification following the execution of the READ statement in another processing statement for the internal table, the same table key should be used there, whereby it is important to note that the primary index is always addressed if there is no explicit key specification.

Alternative 1

... FROM wa [USING KEY keyname]


Effect

For wa, a work area compatible to the row type of the internal table must be specified. This concerns functional operand positions. The first row of the internal table found, whose values in the columns of the table key used match those of the corresponding components of wa, is processed. If the key fields in wa are empty, no entries are processed.

If the USING KEY addition is not specified, the primary table key is used. If the USING KEY addition is specified, the table key specified in keyname is used.

If the primary table key is used to access a standard table and the key is empty, then the first row of the internal table is read. If this is statically identifiable, the syntax check produces a warning.

Notes

Alternative 2

... WITH TABLE KEY [keyname COMPONENTS] ...


Effect

Each component of the table key used must be listed either directly as comp_name1 comp_name2 ... or as a parenthesized character-like data object name1 name2 ..., which contains the name of the component when the statement is executed. name is not case-sensitive. If name only contains blanks, this specified component is ignored when the statement is executed. An operand operand1 operand2 ... compatible with the data type of the component or convertible to it must be assigned to every component. The first row of the internal table found, whose values in the column of the table key used correspond with the values in the operands operand1 operand2 ... assigned, is processed. Duplicate or overlapping keys cannot be specified, nor can columns be specified that are not components of the table key.

operand1 operand2 ... are general expression positions. If necessary, the content of the operands is converted to the data type of the components before the comparison. If an arithmetic expression is specified, the calculation type is determined from its operands and the data type of the component and the result, if necessary, is converted to the data type of the component.

If the addition COMPONENTS is not specified, the primary table key is used. If the addition COMPONENTS is specified, the table key specified in keyname is used.

Notes

Example

For different key inputs, see Key Access.

Example

Reads rows of the internal table spfli_tab using the primary table key and a secondary table key. The first READ statement evaluates the work area spfli_key; the second READ statement specifies the components of the secondary table key city_key explicitly.

DATA: spfli_tab TYPE SORTED TABLE OF spfli
                WITH UNIQUE KEY carrid connid
                WITH NON-UNIQUE SORTED KEY city_key
                                COMPONENTS cityfrom cityto,
      spfli_key LIKE LINE OF spfli_tab.

FIELD-SYMBOLS <spfli> TYPE spfli.

...

spfli_key-carrid = 'LH'.
spfli_key-connid = '0400'.
READ TABLE spfli_tab FROM spfli_key ASSIGNING <spfli>.

IF sy-subrc = 0.
  ...
ENDIF.

...

READ TABLE spfli_tab
           WITH TABLE KEY city_key
                COMPONENTS cityfrom = 'LH' cityto = '2402'
           ASSIGNING <spfli>.

IF sy-subrc = 0.
  ...
ENDIF.