ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → DELETE itab → DELETE itab - itab_line
DELETE itab - index
Other versions:
7.31 | 7.40 | 7.54
Syntax
... { itab INDEX idx [USING KEY keyname] }
| { itab [USING KEY loop_key]} ...
Alternatives
1. ... itab INDEX idx [USING KEY keyname]
2. ... itab [USING KEY loop_key]
Effect
These alternatives specify the rows to be deleted using the specification of a row number relating to a table index.
Alternative 1
... itab INDEX idx
Addition
Effect
If the addition INDEX
is used, the statement DELETE
deletes the row of the row number specified in idx
with respect to a table index. idx
is a
numeric expression
position of the operand type i
. If idx
contains a value of 0 or less, an exception is raised that cannot be handled.
If the addition USING KEY
is not used, the addition INDEX
can only be used with
index tables and determines the row to be deleted from the
primary table index.
Example
Deletes the table row that has the same value as the input field carrid
in the key field carrid
, by using the primary table index.
DATA carrid TYPE scarr-carrid.
cl_demo_input=>request( CHANGING field = carrid ).
DATA scarr_tab TYPE SORTED TABLE OF scarr
WITH UNIQUE KEY carrid.
SELECT *
FROM scarr
INTO TABLE @scarr_tab.
DATA(idx) = line_index( scarr_tab[ KEY primary_key carrid = carrid ] ).
IF idx is NOT INITIAL.
DELETE scarr_tab INDEX idx.
ENDIF.
Executable Example
Addition
... USING KEY keyname
Effect
If the addition USING KEY
is used, a table key can be specified in
keyname
to specify the table index to be used explicitly.
If the table has a sorted
secondary key, this can be specified in keyname
. The row to be deleted is then determined from its
secondary table index. A secondary
hash key cannot be specified.
If the primary table
key is specified under the name primary_key
, the table must be an index table, and the behavior is the same as when USING KEY
is not specified.
Note
If a sorted secondary key exists, the INDEX
addition can be used for all table types, if USING KEY
is used.
Example
The DELETE
statement deletes the third row of the internal table since this
has the row number 1 in the secondary table index belonging to the secondary table key skey
.
DATA itab TYPE TABLE OF i WITH EMPTY KEY
WITH UNIQUE SORTED KEY skey COMPONENTS TABLE_LINE.
itab = VALUE #( ( 3 ) ( 2 ) ( 1 ) ).
DELETE itab INDEX 1 USING KEY skey.
Alternative 2
... itab
Addition
Effect
This variant is only possible within a LOOP
across the same internal table. The current table row of the LOOP
is then
deleted implicitly. If the addition USING
KEY is specified in LOOP
, the variant USING KEY loop_key
must be specified for this variant.
If the current row was already deleted in the same loop, however, the behavior is undefined.
This variant is not allowed outside of a LOOP
and raises a warning in the syntax check if it is not known statically that is is specified in a loop.
Note
This alternative is not recommended. Instead, use the addition INDEX
to specify the row number explicitly.
Example
The following loop deletes all rows in an internal table, since the short form of the statement DELETE
always deletes the current first row.
DATA itab TYPE TABLE OF i WITH EMPTY KEY.
itab = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
DELETE itab.
ENDLOOP.
Addition
... USING KEY loop_key
Effect
This addition is required if the table key used by the LOOP
is specified
explicitly in the statement LOOP
. It states explicitly that the current table
row is deleted by the LOOP
. No other key can be specified apart from the
predefined name loop_key
.
If no explicit table key is specified for LOOP
, the addition USING KEY loop_key
is optional.
Example
Since the primary table key is specified explicitly for the LOOP
loop, USING KEY loop_key
must be specified for DELETE
as well.
DATA itab TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line.
itab = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
LOOP AT itab USING KEY primary_key
ASSIGNING FIELD-SYMBOL(<fs>).
DELETE itab USING KEY loop_key.
ENDLOOP.