Internal Tables - Deleting Rows Using the Index
This example demonstrates how rows can be deleted from internal tables using the index.
DATA itab TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line. FIELD-SYMBOLS <line> LIKE LINE OF itab. DO 5 TIMES. APPEND sy-index TO itab. ENDDO. DELETE itab INDEX: 2, 3, 4. WRITE: 'sy-subrc =', sy-subrc. SKIP. LOOP AT itab ASSIGNING <line>. WRITE: / sy-tabix, <line>. ENDLOOP.
A sorted table
itab is filled with five rows. Then an attempt is made to
delete the rows with the indices 2, 3, and 4 using a chained statement. However, after the deletion
of each individual row, the index of each subsequent row is lowered by 1.This means, the second delete
operation actually deletes the row which originally had the index 4. The third delete operation fails because the table is now made up of only 3 rows.
To actually delete the rows 2 to 4, the
FROM TO addition of the
DELETE statement is used.