ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  DELETE itab → 

Internal Tables - Deleting Rows Using the Index

This example demonstrates how rows can be deleted from internal tables using the index.

Source Code

REPORT demo_int_tables_delete_ind_1.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    DATA itab TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line.

    FIELD-SYMBOLS <line> LIKE LINE OF itab.

    DATA(out) = cl_demo_output=>new( ).

    DO 5 TIMES.
      APPEND sy-index TO itab.
    ENDDO.

    DELETE itab INDEX: 2, 3, 4.

    out->write( |sy-subrc: { sy-subrc }| ).

    LOOP AT itab ASSIGNING <line>.
      out->write( |{ sy-tabix } { <line> }| ).
    ENDLOOP.

    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

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.