Entering content frame

Reading Lines of Tables Locate the document in its SAP Library structure

To read a single line of any table, use the statement:

READ TABLE itab key result.

For the statement to be valid for any kind of table, you must specify the entry using the key and not the index. You specify the key in the key part of the statement. The result part can specify a further processing option for the line that is retrieved.

If the system finds an entry, it sets sy-subrc to zero, if not, it takes the value 4, as long as it is not influenced by one of the possible additions. If the internal table is an index table, sy-tabix is set to the index of the line retrieved. If the table has a non-unique key and there are duplicate entries, the first entry is read.

Specifying the Search Key

The search key may be either the table key or another key.

If you read entries  from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search.

Using the Table Key

To use the table key of itab as a search key, enter key as follows:

READ TABLE itab FROM wa result.

or as follows

READ TABLE itab WITH TABLE KEY k1 = f1 ... kn = fn result.

In the first case, wa must be a work area compatible with the line type of itab. The values of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. It is not allowed to specify the same key specifications several times. If you do not know the name of one of the key fields until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1) = f1 (n2) = f2  If the data types of f1 ... fn are not compatible with the key fields, the system converts them. If the row type of the internal table is not structured, you can specify a comparison with the pseudo-component table_line.

READ TABLE itab WITH TABLE KEY table_line = f result.

The contents of the entire table line are compared with the contents of field f. If f is not compatible with the line type of the table, the value is converted into the line type. The search key allows you to find entries in internal tables that do not have a structured line type, that is, where the line is a single field or an internal table type.

If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as key values using comp->attr or table_line->attr.

The system searches for the relevant table types as follows:

        Standard tables

Linear search, where the runtime is in linear relation to the number of table entries.

        Sorted tables

Binary search, where the runtime is in logarithmic relation to the number of table entries.

        Hashed tables

The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.

Using a Different Search Key

READ TABLE itab WITH KEY k1 = f1 ... kn = fn result.

The search key consists of arbitrary table fields k1 ... kn. If you do not know the name of a component until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1) = f1 (n2) = f2  If n1 ... nn is empty when the statement is executed, the search field is ignored. If the data types of f1 ... fn are not compatible with the components, the system converts them. You can restrict the search to partial fields by specifying offset and length.

If the row type of the internal table is not structured, you can specify a comparison with the pseudo-component table_line. You can also specify the attributes of objects as keys.

The search is linear for all table types. The runtime is in linear relation to the number of table lines.

Specifying the Extra Processing Option

You can specify an option that specifies what the system does with the table entry that it finds.

Using a Work Area

You can write the table entry read from the table into a work area by specifying result as follows:

READ TABLE itab key INTO wa [COMPARING f1 f2 ... |ALL FIELDS]
                            [TRANSPORTING f1 f2 ...
|ALL FIELDS
                                                    |NO FIELDS].

If you do not use the additions COMPARING or TRANSPORTING, the contents of the table line must be convertible into the data type of the work area wa. If you specify COMPARING or TRANSPORTING, the line type and work area must be compatible. You should always use a work area that is compatible with the line type of the relevant internal table.

If you use the COMPARING addition, the specified table fields f1 ... fn of the structured line type are compared with the corresponding fields of the work area before being transported. If you use the ALL FIELDS option, the system compares all components. If the system finds an entry with the specified key key and if the contents of the compared fields are the same, sy-subrc is set to 0. If the contents of the compared fields are not the same, sy-subrc returns the value 2. If the system cannot find an entry, sy-subrc is set to 4. If the system finds an entry, it copies it into the target work area regardless of the result of the comparison.

If you use the TRANSPORTING addition, you can specify the table fields f1 ... fn  of the structured line type that you want to transport into the work area. If you specify ALL FIELDS without TRANSPORTING, the contents of all of the fields are transported. If you specify NO FIELDS, no fields are transported. In the latter case, the READ statement only fills the system fields sy-subrc and sy-tabix. Specifying the work area wa with TRANSPORTING NO FIELDS is unnecessary, and should be omitted.

In both additions, you can also specify the fields f1 ... fn … dynamically using (n1) (n2)… as the contents of a field  n1 ... nn. If n1 ... nn is empty when the statement is executed, it is ignored. You can also restrict all fields f1 ... fn to subfields by specifying offset and length.

Using a Field Symbol

You can assign the table entry read from the table to a field symbol by specifying result as follows:

READ TABLE itab key ASSIGNING <fs>.

After the READ statement, the field symbol points to the table line. If the line type is structured, you should specify the same type for the field symbol when you declare  it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.

For further information about assigning table lines to field symbols, refer to Access Using Field Symbols.

Example

REPORT demo_int_tables_read_comparing.

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.

DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.

ENDDO.

line-col1 = 2.
line-col2 = 3.

READ TABLE itab FROM line INTO line COMPARING col2.

WRITE: 'sy-subrc =', sy-subrc.
SKIP.
WRITE: / line-col1, line-col2.

The list output is:

sy-subrc =    2

         2        4

The program fills a hashed table with a list of square numbers. The work area LINE, which is compatible with the line type, is filled with the numbers 2 and 3. The READstatement reads the line of the table in which the key field col1has the same value as in the work area and copies it into the work area. sy-subrcequals 2, because different numbers are found when compared to the field col2.

Example

REPORT demo_int_tables_read_transport .

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.

DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.

ENDDO.

CLEAR line.

READ TABLE itab WITH TABLE KEY col1 = 3
                INTO line TRANSPORTING col2.

WRITE:   'sy-subrc =', sy-subrc,
       / 'sy-tabix =', sy-tabix.
SKIP.
WRITE: / line-col1, line-col2.

The list output is:

sy-subrc=    0
sy-tabix =       3

         0        9

The program fills a sorted table with a list of square numbers. The READ statement reads the line of the table in which the key field col1 has the value 3. Only the content of col2is copied to the work area LINE. sy-subrc has the value zero and sy-tabix has the value 3, because itabis an index table.

Example

REPORT demo_int_tables_read_transp_no .

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.

DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.

ENDDO.

READ TABLE itab WITH KEY col2 = 16  TRANSPORTING NO FIELDS.

WRITE:   'sy-subrc =', sy-subrc,
       / 'sy-tabix =', sy-tabix.

The list output is:

sy-subrc =    0
sy-tabix =       4

The program fills a sorted table with a list of square numbers. The READ statement reads the line of the table in which the key field col2 has the value 16. It does not use the table key. No fields are copied to a work area or assigned to a field symbol. sy-subrcis zero, since a line was found, and sy-tabix is four.

Example

REPORT demo_int_tables_read_assigning .

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE HASHED TABLE OF line WITH UNIQUE KEY col1.

FIELD-SYMBOLS <fs> LIKE LINE OF itab.

DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  INSERT line INTO TABLE itab.

ENDDO.

READ TABLE itab WITH TABLE KEY col1 = 2 ASSIGNING <fs>.

<fs>-col2 = 100.

LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.

The list output is:

         1        1
         2      100
         3        9
         4       16

The program fills a hashed table with a list of square numbers. The READ statement reads the line of the table in which the key field col1 has the value 2 and assigns it to the field symbol <fs> . The program then assigns the value 100 to component col2 of <fs> . This also changes the corresponding table field.

 

Reading Lines Using the Index

You can use the READ statement to read lines in tables using their index. To read a single line of an index table, use the statement:

READ TABLE itab INDEX idx result.

The system reads the line with the index idx from the table itab. This is quicker than searching using the key. The result part can specify a further processing option for the line that is retrieved.

If an entry with the specified index was found, the system field sy-subrc is set to 0 and sy-tabixcontains the index of that line. Otherwise, sy-subrc is set to a value other than 0.

If idx is less than or equal to 0, a runtime error occurs. If idx is greater than the number of lines in the table, sy-subrc is set to 4.

Example

REPORT demo_int_tables_read_index.

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.

FIELD-SYMBOLS <fs> LIKE LINE OF itab.

DO 20 TIMES.
  line-col1 = sy-index.
  line-col2 = 2 * sy-index.
  APPEND line TO itab.
ENDDO.

READ TABLE itab ASSIGNING <fs> INDEX 7.

WRITE:   sy-subrc, sy-tabix.
WRITE: / <fs>-col1, <fs>-col2.

The list output is:

    0         7
         7         14

The example creates a sorted table itaband fills it with 20 lines. The line with index 7 is read and assigned to the field symbol <fs>.

 

Leaving content frame