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

READ TABLE - free_key

Short Reference

Syntax

... WITH KEY { comp1 = operand1 comp2 = operand2 ... [BINARY SEARCH] }
          | { keyname COMPONENTS comp1 = operand1 comp2 = operand2 ... } ... .

Variants:

1. ... WITH KEY comp1 = operand1 comp2 = operand2 ... [BINARY SEARCH] ... .

2. ... WITH KEY keyname COMPONENTS comp1 = operand1 comp2 = operand2 ... .

Effect

Specifies a free search key. The free search key can be defined freely or linked to the specification of a secondary table key in keyname.

Notes

Variant 1

... WITH KEY comp1 = operand1 comp2 = operand2 ... [BINARY SEARCH] ... .


Addition:

... BINARY SEARCH

Effect

Components comp1 comp2 ... can be declared as search keys behind the WITH KEY addition, following the rules here. An operand operand1 operand2 ... is assigned to each of these search keys and must be compatible with the data type of the component (or convertible to this data type). No duplicate or overlapping key declarations can be made.

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.

The first row of the internal table is searched for whose values in the specified components (or their subareas or attributes) match the values in the assigned operands operand1 operand2 ...

The search runs as follows for the individual table types, without BINARY SEARCH being specified:

If the name field of a component comp is initial, the first row that matches the search key is read. If all name fields are initial, the first row of the internal table is read.

The system field sy-tabix is set in accordance with the table type:

Notes

Example

The internal table html_viewer_tab contains references to HTML controls. The READ statement reads the reference that points to a HTML control in a specific container control.

DATA: container TYPE REF TO cl_gui_container,
      html_viewer TYPE REF TO cl_gui_html_viewer.

DATA html_viewer_tab LIKE TABLE OF html_viewer.

...

CREATE OBJECT html_viewer EXPORTING parent = container.
APPEND html_viewer TO html_viewer_tab.

...

READ TABLE html_viewer_tab
           WITH KEY table_line->parent = container
           INTO html_viewer.

...

Addition

... BINARY SEARCH

Effect

The addition BINARY SEARCH produces a binary search of the table, not linear. In the case of large tables (from approximately 100 entries), this can significantly reduce runtime. The table must, however, be sorted in ascending order by the components specified in the search key. The priority of the sort order must match exactly the order of the components in the search key. If this requirement is not met, the correct row is not usually found.

Notes

Variant 2

... WITH KEY keyname COMPONENTS comp1 = operand1 comp2 = operand2 ... .


Effect

keyname can be used to declare a table key. The same applies to the declaration of the components as in the variant without key declaration.

If a secondary table key is declared in keyname, the behavior is as follows:

The system field sy-tabix is set with respect to the specified secondary table key:

If the primary table key is declared in keyname under the name primary_key, the behavior is the same as in the variant without key declaration.

Notes

Example

The DEMO_SECONDARY_KEYS program demonstrates the specification of a secondary table key compared to the completely free specification of a key and the resulting performance benefits.