SAP® Documentation

Single view

ABAPREAD_TABLE - READ TABLE

RFUMSV00 - Advance Return for Tax on Sales/Purchases   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.
SAP E-Book

READ TABLE itab

Short Reference



Syntax

READ TABLE itab ${ table_key
                $| free_key
                $| index $} result.


Effect

This statement reads a row from the internal table itab. itab is a functional operand position.

The row must be specified by naming values for either table_key for a table key, a free condition free_key, or an index index. The latter is possible only for index tables and when using a sortedsecondary key. The output response result determines how and to where the row contents are read.

If the row to be read is not specified uniquely, the first suitable row is read. In the case of index tables, this row has the lowest row number of all suitable rows in the table index used.

If the internal table is specified as the return value or result of a functional method, a constructor expression, or a table expression, the value is only available when the statement is executed. Afterwards, it is no longer possible to access the internal table.

System Fields

The statement READ TABLE sets the values for the system fields sy-subrc and sy-tabix.

sy-subrcMeaning
0Row is found. sy-tabix is set to the row number of the entry in the primary or secondary table index used. If a hash key is used, it is set to 0.
2Like sy-subrc equals 0. Distinguishes cases that use the addition COMPARING in result.
4The row was not found. If the entry was determined using a binary search, sy-tabix is set to the table index of the entry before which it would be inserted using INSERT ... INDEX ..., to preserve the sort order. This is the case if, when sorted keys are used, the addition table_key or free_key was specified alongside the full table key or if the addition BINARY SEARCH was specified explicitly. Otherwise, sy-tabix is undefined (-1).
8The row was not found. The entry was found using a binary search and the end of the table was reached. sy-tabix is set to the number of rows + 1.

The system fields sy-tfill and sy-tleng are also filled.

Notes

  • There is no implicit selection of a suitable key or index. The used table key or table index is always specified uniquely. The syntax check issues a warning if there is a suitable secondary table key but this table key is not used. This warning should be removed through using the key. However, in exceptional cases, it can be bypassed using a pragma.
  • As an alternative to the statement READ TABLE, table expressions can be used to allow reads to be performed on individual table rows at operand positions.
  • Internal tables can also be specified as a data source of a query in .
  • If multiple rows of an internal table are to be read, the statement LOOP generally displays better performance than using the statement READ TABLE in a loop.


Example

Reads individual table rows from a standard table in a WHILE loop. The table rows are read in reverse order with respect to the sorted secondary key sort_key. A CHECK statement exits the loop if the specified condition is not met. This construct provides the missing option of performing LOOPs in reverse order. See also the corresponding executable example with a FOR expression.

DATA itab TYPE STANDARD TABLE OF i
          WITH EMPTY KEY
          WITH NON-UNIQUE SORTED KEY sort_key COMPONENTS table_line.

itab = VALUE #( ( 2 ) ( 5 ) ( 1 ) ( 3 ) ( 4 ) ).

DATA(output) = ``.
DATA(idx) = lines( itab ).
WHILE idx > 0.
  READ TABLE itab INDEX idx USING KEY sort_key
             ASSIGNING FIELD-SYMBOL(<fs>).
  idx = idx  - 1.
  CHECK <fs> > 2.
  output = output && <fs> && ` `.
ENDWHILE.

cl_demo_output=>display( output ).

Exceptions

Non-Catchable Exceptions

  • Cause: When reading a table with READ ... WITH $[TABLE$] KEY, overlapping or duplicate keys were specified.
    Runtime Error:DYN_KEY_DUPLICATE
  • Cause: When reading a table of the type SORTED, the specified key fields have to be an initial part of the table key when BINARY SEARCH is specified.
    Runtime Error:ITAB_ILLEGAL_BINARY_SEARCH
  • Cause: No key specified.
    Runtime Error:ITAB_KEY_COMPONENT_MISSING
  • Cause: Invalid key specified when accessing a key table.
    Runtime Error:ITAB_KEY_ILLEGAL_COMPONENT
  • Cause: Key specified implicitly (invalid)
    Runtime Error:READ_ITAB_UC_KEY_ERROR
  • Cause: Invalid incompatible work area specified.
    Runtime Error:OBJECTS_WA_NOT_COMPATIBLE
  • Cause:Memory area violated when TABLES parameter accessed
    Runtime Error:ITAB_STRUC_ACCESS_VIOLATION







BAL Application Log Documentation   SUBST_MERGE_LIST - merge external lists to one complete list with #if... logic for R3up  
This documentation is copyright by SAP AG.

Length: 10116 Date: 20200126 Time: 032608     sap01-206 ( 44 ms )

Our Service

Looking for Support? Questions?

The

Consolut

Callback-Service

Leave us your contact details and we will call you back. Panels marked with * are mandatory.