AS ABAP Release 758, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing Internal Data → Internal Tables (itab) → itab - Processing Statements → READ TABLE itab →
READ TABLE, result
Syntax
... { INTO wa [transport_options] }
| { ASSIGNING <fs> [CASTING] [ELSE UNASSIGN] }
| { REFERENCE INTO dref }
| { TRANSPORTING NO FIELDS }.
1. ... INTO wa [transport_options]
2. ... ASSIGNING <fs> [CASTING] [ELSE UNASSIGN]
3. ... REFERENCE INTO dref
4. ... TRANSPORTING NO FIELDS
Programming Guideline
Effect
Defines the output behavior of a READ statement for an internal table. There are four alternatives for the output behavior:
Hint
Outside of classes, the addition INTO can also be specified together with TRANSPORTING NO FIELDS, but this produces a warning in the syntax check
... INTO wa [transport_options]
Effect
The content of the found line is assigned to the work area wa. The following can be specified for wa:
If no line is found, wa remains unchanged or initial. If a conversion error occurs in the assignment to wa, the exception cannot be handled using CX_SY_CONVERSION_ERROR and the associated runtime error occurs instead.
If the additions transport_options are used, the work area wa must be compatible with the line type of the internal table.
Hint
For READ TABLE, an obsolete short form exists where INTO wa can be omitted if the internal table has an identically named
header line itab.
INTO itab is then implicitly added to the statement. This short form is independent of the
obsolete key specification, which also evaluates the header line.
Example
Reading of a particular line in the internal table sflight_tab and assignment to a work area sflight_wa declared inline. After a successful assignment, the content of a component of the line is changed in the internal table.
DATA: carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate.
...
DATA sflight_tab TYPE SORTED TABLE OF sflight
WITH UNIQUE KEY carrid connid fldate.
SELECT *
FROM sflight
WHERE carrid = @carrid AND
connid = @connid
INTO TABLE @sflight_tab.
IF sy-subrc = 0.
READ TABLE sflight_tab
WITH TABLE KEY carrid = carrid
connid = connid
fldate = fldate
INTO DATA(sflight_wa).
IF sy-subrc = 0.
sflight_wa-price *= '0.9'.
MODIFY sflight_tab FROM sflight_wa INDEX sy-tabix.
ENDIF.
ENDIF.
... ASSIGNING <fs> [CASTING] [ELSE UNASSIGN]
1. ... CASTING
2. ... ELSE UNASSIGN
Effect
The found table line is assigned to a field symbol. After the statement READ TABLE, the field symbol points to the table line in the memory. The addition cannot be specified if itab is specified as the return value or result of a functional method, a constructor expression, or a table expression, since this value no longer exists once the statement has been executed.
The following can be specified for <fs>:
If no table line is found, the state of <fs> depends on the addition ELSE UNASSIGN.
As long as the field symbol points to the line, assignments to the field symbol modify the line in the internal table. The following restrictions apply with respect to modifications to key fields of the primary and secondary table keys:
The administration of unique secondary keys is updated after modifications are made to individual lines using field symbols the next time the internal table is accessed
(delayed update). The administration of non-unique secondary keys is updated after the next explicit use of the secondary key
(lazy update). The check
on the uniqueness of a secondary key does not take place until the time of the update. An internal table
might therefore be in an inconsistent state with respect to the secondary key after individual lines
have been modified using field symbols, which does not raise an exception until the table is next used.
If the next use is not directly after the modification, the secondary key can be explicitly updated using methods of the class CL_ABAP_ITAB_UTILITIES
to handle any exceptions on the spot.
Hints
... CASTING
Effect
The addition CASTING has the same meaning as if it were specified without further additions in the statement ASSIGN: The field symbol must be either completely typed, or typed with one of the generic built-in ABAP types c, n, p, or x. The assigned table line is cast to the type of the field symbol. The same exceptions can be raised here as with ASSIGN.
The addition CASTING cannot be used if the field symbol is declared inline with FIELD-SYMBOL.
... ELSE UNASSIGN
Effect
The addition ELSE UNASSIGN sets the state of the field symbol to unassigned if no table line is found and sy-subrc is set to 4 or 8. If ELSE UNASSIGN is not specified, the field symbol keeps its previous state.
Hint
See also the addition ELSE UNASSIGN of the statement ASSIGN.
Example
Reading of a particular line in the internal table sflight_tab and assignment to a field symbol <sflight> declared inline. After a successful assignment, the content of a component of the line is changed in the internal table. See also the example of the assignment of a table expression to a field symbol.
DATA: carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate.
...
DATA sflight_tab TYPE SORTED TABLE OF sflight
WITH UNIQUE KEY carrid connid fldate.
SELECT *
FROM sflight
WHERE carrid = @carrid AND
connid = @connid
INTO TABLE @sflight_tab.
IF sy-subrc = 0.
READ TABLE sflight_tab
WITH TABLE KEY carrid = carrid
connid = connid
fldate = fldate
ASSIGNING FIELD-SYMBOL(<sflight>) ELSE UNASSIGN.
IF <sflight> IS ASSIGNED.
<sflight>-price *= '0.9'.
ENDIF.
ENDIF.
... REFERENCE INTO dref
Effect
A reference to the found table line is made in the data reference variable dref. The addition cannot be specified if itab is specified as the return value or result of a functional method or of a table expression of a constructor expression, since this value no longer exists once the statement has been executed.
The following can be specified for dref:
If no table line is found, dref remains unchanged or initial.
By dereferencing the data reference, the content of the found table line can be evaluated and changed. The same restrictions apply to the modification of key fields of the
primary and
secondary table key as to access using field symbols (see ASSIGNING addition).
Hints
Example
Reading of a particular line of the internal table sflight_tab and assignment of a reference to the found line to the data reference variable sflight_ref (declared inline). After a successful assignment, the content of a component of the line is changed in the internal table.
DATA: carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate.
...
DATA sflight_tab TYPE SORTED TABLE OF sflight
WITH UNIQUE KEY carrid connid fldate.
SELECT *
FROM sflight
WHERE carrid = @carrid AND
connid = @connid
INTO TABLE @sflight_tab.
IF sy-subrc = 0.
READ TABLE sflight_tab
WITH TABLE KEY carrid = carrid
connid = connid
fldate = fldate
REFERENCE INTO FINAL(sflight_ref).
IF sy-subrc = 0.
sflight_ref->price *= '0.9'.
ENDIF.
ENDIF.
... TRANSPORTING NO FIELDS
Effect
If the addition TRANSPORTING NO FIELDS is used, the statement READ TABLE only checks whether the line that is being searched for exists and fills the system fields sy-subrc and sy-tabix. The system cannot access the content of the found line.
Hints
Example
Check whether a particular line exists in the internal table sflight_carr and assignment of the line number in the primary table index of the found line in sy-tabix to idx.
DATA carrid TYPE scarr-carrid.
...
DATA: scarr_tab TYPE SORTED TABLE OF scarr
WITH UNIQUE KEY carrid,
idx TYPE i.
SELECT *
FROM scarr
INTO TABLE @scarr_tab.
READ TABLE scarr_tab
WITH TABLE KEY carrid = carrid
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
idx = sy-tabix.
ENDIF.
itab - Output Area