ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → READ TABLE itab
READ TABLE - result
Other versions: 7.31 | 7.40 | 7.54
Syntax
... { INTO wa [transport_options] }
| { ASSIGNING <fs> [CASTING] }
| { REFERENCE INTO dref }
| { TRANSPORTING NO FIELDS }.
Alternatives
1. ... INTO wa [transport_options]
2. ... ASSIGNING <fs> [CASTING]
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:
-
The addition
INTO
assigns the content of the found row to a work area. This addition can be used regardless of which wayitab
is specified. -
The addition
ASSIGNING
assigns the found row to a field symbol<fs>
. This addition is possibly only if an existing internal table is specified foritab
. -
The addition
REFERENCE INTO
creates a reference to the found row in a reference table. This addition is possibly only if an existing internal table is specified foritab
. -
The addition
TRANSPORTING NO FIELDS
specifies that only the relevant system fields are filled. This addition can be used regardless of which wayitab
is specified.
Note
Outside of classes, the addition INTO
can also be specified together with TRANSPORTING NO FIELDS
, but this produces a warning in the syntax check
Alternative 1
... INTO wa [transport_options]
Effect
The content of the found row is assigned to the work area wa
. The following can be specified for wa
:
- An existing work area that matches the row type of the internal table. The row type must be compatible with the data type of the work area or must be able to be converted to this type. If the work area is incompatible with the row type of the internal table, the content of the table row is converted to the data type of the work area in accordance with the conversion rules.
-
An inline declaration
DATA(var)
, where a work area with the row type of the internal table is declared. The row type must be known statically and completely.
If no row 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 row type of the internal table.
Note
For READ TABLE
, an (external)
obsolete short form exists where INTO wa
can be omitted if the internal table has a
header line itab
with the same name. The statement is then implicitly enhanced by the addition of INTO itab
. This short form is unrelated to the
obsolete key specification, which also evaluates the header line.
Example
Reads a particular row in the internal table sflight_tab
and assigns it to
a work area sflight_wa
(declared inline). After the reference has been successfully assigned, the content of a component of the row 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.
Alternative 2
... ASSIGNING <fs> [CASTING]
Effect
The found table row is assigned to a field symbol. After the statement READ TABLE
,
the field symbol points to the table row 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>
:
-
An existing field symbol whose typing matches the row type of the internal table. The optional addition
CASTING
can be used to perform a casting. It has the same meaning as if it were specified in the statementASSIGN
: The field symbol must be either fully typed, or typed with one of the generic built-in ABAP types c,n
,p
, orx
. The assigned table row is cast to the type of the field symbol. The same exceptions can be raised here as withASSIGN
. -
An inline declaration
FIELD-SYMBOL(<fs>)
, where a field symbol with the row type of the internal table is declared. If this cannot be known statically, the field symbol is declared with the generic typeany
and is assigned the constantspace
initially. The inline declaration cannot be specified after the additionCASTING
.
If no table row is found, <fs>
remains unchanged or initial.
As long as the field symbol points to the row, assignments to the field symbol modify the row in the internal table. The following restrictions apply with respect to modifications to key fields of the primary and secondary table keys:
- The key fields of the primary table key of sorted tables and hashed tables are read-only and must not be modified. This would invalidate internal table administration. The processing statements for internal tables check whether writes are performed on individual key fields and a corresponding non-handleable exception raised. If writes are performed in write positions across the entire table row (for example, as a target field of assignments or as actual parameters for output parameters), an exception is always raised.
-
The key fields of a secondary
table key, however, are only read-only while the secondary table is being used. This is the case
in
LOOP
loops and during the use of theMODIFY
statement, in which the secondary key is specified afterUSING KEY
. Otherwise the key fields are not read-only.
The administration of unique secondary keys is updated after modifications are made to individual rows 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 rows are modified using field symbols. An exception is not raised 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 CL_ABAP_ITAB_UTILITIES class to handle possible exceptions on the spot.
Notes
- The typing of the field symbol must be compatible with the row type of the internal table.
-
If the
READ
statement is successful (sy-subrc
has value 0), it is guaranteed that the field symbol immediately after execution of the statement indicates a memory area. A query usingIS ASSIGNED
is not necessary there. -
It is safer to evaluate the return code
sy-subrc
than to useIS ASSIGNED
, since a memory area can be assigned to field symbol. This applies to inline declarations too. -
If the row to which the field symbol points is deleted, no more memory space is allocated to the field
symbol and it can no longer be used instead of a data object. If the field symbol is not used directly
after the
READ
statement, it may be useful to carry out a check usingIS ASSIGNED
. -
Another form of the statement
READ TABLE
using the additionASSIGNING
is a table expressiontable_exp
specified after the statementASSIGN
.
Example
Selects a particular row in the internal table sflight_tab
and assigns it
to a field symbol <sflight>
(declared inline). After the reference has
been successfully assigned, the content of a component of the row 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>).
IF sy-subrc = 0.
<sflight>-price *= '0.9'.
ENDIF.
ENDIF.
Alternative 3
... REFERENCE INTO dref
Effect
A reference to the found table row 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
:
-
An existing data reference variable whose static type is compatible with the row type of the internal table or is the generic type
data
. -
An inline declaration
DATA(var)
, where a data reference variable is declared whose static type is the row type of the internal table. The row type must be known statically and completely.
If no table row is found, dref
remains unchanged or initial.
By dereferencing the data reference, the content of the found table row can be evaluated and changed. The same limitations apply to the modification of key fields of the
primary and
secondary table key as for access using field symbols (see ASSIGNING
addition).
Notes
-
If the
READ
statement is successful (sy-subrc
has value 0), it is guaranteed that the data reference variable immediately after the execution of the statement indicates a row. A query usingIS BOUND
is not necessary here. -
Alongside
GET REFERENCE
and the reference operatorREF
,REFERENCE INTO
is the only way of creating stack references. Stack references can become invalid if the referenced data object is deleted. -
When applied to internal tables in the heap
REFERENCE INTO
creates memory-retaining heap references. -
All references (heap references and
stack references) that
point to rows from internal tables can become invalid when rows are deleted. If a data reference variable
is not used directly after the
READ
statement, it may be useful to carry out a check usingIS BOUND
.
Example
Selects a particular row of the internal table sflight_tab
and assigns a
reference to the found row to the data reference variable sflight_ref
(declared
inline). After the reference has been successfully assigned, the content of a component of the row 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 DATA(sflight_ref).
IF sy-subrc = 0.
sflight_ref->price *= '0.9'.
ENDIF.
ENDIF.
Alternative 4
... TRANSPORTING NO FIELDS
Effect
If the addition TRANSPORTING NO FIELDS
is used, the statement READ
TABLE only checks whether the row 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 row.
Notes
-
The predicate function
line_exists
can also be used to check the existence of a table row. -
The table function
line_index
can also be used to identify the row number in the table index.
Example
Checks whether a particular row exists in the internal table sflight_carr
and assigns the row number in the primary table index of the found row 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.