ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → DELETE itab → DELETE itab - duplicates
Internal Tables, Deleting Duplicate Rows
This example demonstrates how adjacent duplicate rows in internal tables can be deleted.
Other versions: 7.31 | 7.40 | 7.54
Source Code
DATA: BEGIN OF line,
col1 TYPE i,
col2 TYPE c LENGTH 1,
END OF line.
DATA itab LIKE STANDARD TABLE OF line
WITH NON-UNIQUE KEY col2.
itab = VALUE #( ( col1 = 1 col2 = 'A' )
( col1 = 1 col2 = 'A' )
( col1 = 1 col2 = 'B' )
( col1 = 2 col2 = 'B' )
( col1 = 3 col2 = 'B' )
( col1 = 4 col2 = 'B' )
( col1 = 5 col2 = 'A' ) ).
DATA(out) = cl_demo_output=>new(
)->write_data( itab ).
DELETE ADJACENT DUPLICATES FROM itab COMPARING ALL FIELDS.
out->write_data( itab ).
DELETE ADJACENT DUPLICATES FROM itab COMPARING col1.
out->write_data( itab ).
DELETE ADJACENT DUPLICATES FROM itab.
out->write_data( itab
)->display( ).
Description
The method main
creates and fills a standard table. The first DELETE
statement deletes the second row since it has the same content as the first row. The second DELETE
statement deletes the second row from the remaining table since the content of the field col1
is the same as in the first row. The third DELETE
statement deletes the third
and fourth rows of the remaining table since the content of the key field col2
is the same as in the second row. Although the content of the key fields for the first and fifth rows is the same, the fifth row is not deleted because it is not adjacent to the first row.