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 → DELETE itab →
DELETE itab, itab_lines
Syntax
... itab [USING KEY
keyname] [FROM idx1] [TO idx2]
[STEP n]|[WHERE
log_exp|(cond_syntax)] ...
1. ... USING KEY keyname
2. ... [FROM idx1] [TO idx2]
3. ... STEP
n
4. ... WHERE log_exp
5. ... WHERE (cond_syntax)
Effect
To delete multiple lines, at least one of the additions FROM, TO, STEP, or WHERE must be specified. USING KEY keyname is used to determine the table key to which the additions refer.
If multiple additions are specified, the lines that result from the intersection of the individual additions are deleted.
... USING KEY keyname
Effect
The addition USING KEY can be used to specify a table key in keyname with which the processing is executed. The specified table key affects the order in which the table lines are accessed, and the evaluation of the remaining conditions.
If the primary table key is specified, the processing behaves in the same way as if no key were explicitly specified. If a secondary table key is specified, the order in which the lines are accessed is as follows:
The lines are processed by ascending line number in the secondary table index
The lines are processed in the order in which they were inserted into the table.
Hints
Example
The statement DELETE deletes the first three lines of the internal table itab because they occur from line 4 in the secondary table index of the secondary key skey.
DATA itab TYPE TABLE OF i WITH EMPTY KEY
WITH UNIQUE SORTED KEY skey COMPONENTS table_line.
itab = VALUE #( ( 6 ) ( 5 ) ( 4 ) ( 3 ) ( 2 ) ( 1 ) ).
DELETE itab USING KEY skey FROM 4.
... [FROM idx1] [TO idx2]
Effect
These optional additions have the effect that only table rows from row number idx1 or up to and including row number idx2 are respected in the table index used. The table index used is either the primary index or a secondary sorted index specified by USING KEY.
If only FROM is specified, all lines of the table from line number idx1 up to and including the last line are respected. If only TO is specified, all lines in the table from the first line up to line number idx2 are respected.
idx1 and idx2 are numeric expression positions of operand type i.
The following restrictions apply:
The additions FROM and TO can be specified together with STEP and then special rules apply.
Hint
The statement DELETE FROM itab has the statement
DELETE FROM dbtab with identical syntax. If an internal table has the same name as a database table, a statement like this accesses the internal table.
Example
Deletion of all lines in an internal table from line 4. The result is the same as in the example for APPEND ..., SORTED BY.
DATA: carrid TYPE sflight-carrid VALUE 'LH',
connid TYPE sflight-connid VALUE '0400'.
cl_demo_input=>new(
)->add_field( CHANGING field = carrid
)->add_field( CHANGING field = connid )->request( ).
DATA: BEGIN OF seats,
fldate TYPE sflight-fldate,
seatsocc TYPE sflight-seatsocc,
seatsmax TYPE sflight-seatsmax,
seatsfree TYPE sflight-seatsocc,
END OF seats.
DATA seats_tab LIKE STANDARD TABLE OF seats.
SELECT fldate, seatsocc, seatsmax, seatsmax - seatsocc AS seatsfree
FROM sflight
WHERE carrid = @carrid AND
connid = @connid
INTO TABLE @seats_tab.
SORT seats_tab BY seatsfree DESCENDING.
DELETE seats_tab FROM 4.
cl_demo_output=>display( seats_tab ).
... STEP n
Effect
The optional addition STEP n defines the step size for processing an internal table. The step size is defined by the value of n which must be positive. n is a numeric expression position of operand type i. If the addition STEP is not specified, the step size is 1.
The step size can be combined with the additions FROM and TO and then works on the subset of table lines defined by these conditions. If FROM idx1 and TO idx2 are combined with STEP, idx1 needs to be less than or equal to idx2.
Depending on the value of n, every nth line is processed. The value of n must not be 0. For more details, see LOOP AT itab, cond.
Hints
Example
The following example shows that every second table line between line 1 and 8 is deleted from the internal table.
SELECT *
FROM spfli
ORDER BY carrid
INTO TABLE @DATA(itab).
DELETE itab FROM 1 TO 8 STEP 2.
cl_demo_output=>display( itab ).
Uncatchable Exceptions
... WHERE log_exp
Effect
Static WHERE condition. All lines are processed for which the condition after WHERE is met. If a static WHERE condition is specified, the line type of the internal table must be known statically. WHERE can be specified for all table categories.
A logical expression log_exp can be specified after WHERE, in which the first operand of each relational expression is a component of the internal table. The following can be specified as relational expressions:
No other predicates can be specified. The components of the internal table must be specified as individual operands and not as part of an expression. Parenthesized character-like data objects cannot be used to specify a component dynamically here. The remaining operands of a relational expression are general expression positions at which any suitable individual operands or expressions can be specified, but no components of the internal table. The specified components can have any data type. The corresponding comparison rules apply to the evaluation. Here, a different rule applies to a string expression on the right side than to general logical expressions.
If an optimization is not possible, the behavior is as follows:
Hints
Example
Optimized deletion of a line by specifying the unique primary table key and multiple lines by specifying a non-unique secondary table key in two DELETE statements.
DATA spfli_tab TYPE SORTED TABLE OF spfli
WITH UNIQUE KEY carrid connid
WITH NON-UNIQUE SORTED KEY skey COMPONENTS cityfrom cityto.
SELECT *
FROM spfli
INTO TABLE @spfli_tab.
DELETE spfli_tab WHERE carrid = 'LH' AND
connid = '0400'.
DELETE spfli_tab USING KEY skey WHERE cityfrom = 'FRANKFURT' AND
cityto = 'NEW YORK'.
Executable Example
... WHERE (cond_syntax)
Effect
Dynamic WHERE condition. For cond_syntax any character-like data object or standard table with character-like line type can be specified that, when the statement is executed and with the following exceptions, contains the syntax of a logical expression in accordance with the rules of the static WHERE condition or is initial. The following are not supported in a dynamic WHERE condition:
The syntax in cond_syntax is not case-sensitive as in the static syntax. When an internal table is specified, the syntax can be distributed across multiple lines. If cond_syntax is initial when the statement is executed, the logical expression is true. Invalid logical expressions raises an exception from the class CX_SY_ITAB_DYN_LOOP.
Security Hint
If used incorrectly, dynamic programming techniques can present a serious security risk. Any dynamic content that is passed to a program from the outside must be checked thoroughly or escaped before it is used in dynamic statements. This can be done using the system class CL_ABAP_DYN_PRG or the built-in function escape. See Security Risks Caused by Input from Outside.
Hint
The dynamic WHERE condition is not evaluated for an empty table for optimization
reasons. Therefore, if an internal table is empty, and a logical expression has errors, no exception is raised.
Example
Deletion of the lines of an internal table using a condition that can be entered for the table lines.
DATA condition TYPE string VALUE '<= 0'.
cl_demo_input=>request( CHANGING field = condition ).
condition = `table_line ` && condition.
DATA itab TYPE TABLE OF i WITH EMPTY KEY
WITH NON-UNIQUE SORTED KEY skey COMPONENTS table_line.
FINAL(t) = CONV i( cl_demo_date_time=>get_utc_time( ) ).
FINAL(rnd) = cl_abap_random_int=>create( seed = t
min = 1
max = 20 ).
itab = VALUE #( FOR i = 1 UNTIL i > 100
( rnd->get_next( ) - 10 ) ).
TRY.
DELETE itab USING KEY skey WHERE (condition).
cl_demo_output=>display( itab ).
CATCH cx_sy_itab_dyn_loop INTO FINAL(exc).
...
ENDTRY.
itab - Deleting Lines Using WHERE