Entering content frameDeleting Lines Locate the document in its SAP Library structure

To delete a single line of any internal table, use the DELETE statement. You can either use the table key to find and delete a single line using its key, delete a set of lines that meet a condition, or find and delete neighboring duplicate entries. If the table has a non-unique key and there are duplicate entries, the first entry is deleted.

Deleting a Line Using the Table Key

To use the table key of table <itab> as a search key, use one of the following statements:

DELETE TABLE <itab> FROM <wa>.

or

DELETE TABLE <itab> WITH TABLE KEY <k1> = <f 1> ... <k n> = <f n>.

In the first case, <wa> must be a work area compatible with the line type of <itab>. The values of the key fields are taken from the corresponding components of the work area.

In the second case, you have to supply the values of each key field explicitly. If you do not know the name of one of the key fields until runtime, you can specify it as the content of a field <n i > using the form (<n i >) = <f i >. If the data types of <f i > are not compatible with the key fields, the system converts them.

The system searches for the relevant lines as follows:

Linear search, where the runtime is in linear relation to the number of table entries.

Binary search, where the runtime is in logarithmic relation to the number of table entries.

The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.

If the system finds a line, it deletes it from the table and sets SY-SUBRC to zero. Otherwise, SY-SUBRC is set to 4. If the table has a non-unique key and the system finds duplicate entries, it deletes the first entry.

Deleting Several Lines Using a Condition

To delete more than one line using a condition, use the following statement:

DELETE <itab> WHERE <cond>.

This processes all of the lines that meet the logical condition <cond>. The logical condition can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression TABLE LINE. The comparison then applies to the entire line. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

Deleting Adjacent Duplicate Entries

To delete adjacent duplicate entries use the following statement:

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>
                                  [COMPARING <f1> <f 2> ...
                                             |ALL FIELDS].

The system deletes all adjacent duplicate entries from the internal table <itab>. Entries are duplicate if they fulfill one of the following compare criteria:

You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.

If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

Examples

Example

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL1 = 1.

DELETE TABLE ITAB: FROM LINE,
WITH TABLE KEY COL1 = 3.

LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         2        4
         4       16

The program fills a hashed table with a list of square numbers. The DELETE statement delete the lines from the table where the key field COL1 has the contents 1 or 3.

Example

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

DELETE ITAB WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1        1
         4       16

The program fills a hashed table with a list of square numbers. The DELETE statement deletes the lines of the table where the content of field COL2 is greater than 1 and the content of field COL1 is less than 4.

Example

DATA OFF TYPE I.

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE C,
END OF LINE.

DATA ITAB LIKE STANDARD TABLE OF LINE
          WITH NON-UNIQUE KEY COL2.

LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.
LINE-COL1 = 1. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 2. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 3. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 4. LINE-COL2 = 'B'. APPEND LINE TO ITAB.
LINE-COL1 = 5. LINE-COL2 = 'A'. APPEND LINE TO ITAB.

OFF = 0. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING ALL FIELDS.

OFF = 14. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING COL1.

OFF = 28. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB.

OFF = 42. PERFORM LIST.

FORM LIST.
  SKIP TO LINE 3.
  LOOP AT ITAB INTO LINE.
     WRITE: AT /OFF  LINE-COL1, LINE-COL2.
  ENDLOOP.
ENDFORM.

The output is:

         1 A          1 A          1 A          1 A

         1 A          1 B          2 B          2 B

         1 B 2 B 3 B 5 A

         2 B          3 B          4 B

         3 B          4 B          5 A

         4 B 5 A

         5 A

The example creates and fills a standard table. Here, the first DELETE statement deletes the second line from ITAB because the second line has the same contents as the first line. The second DELETE statement deletes the second line from the remaining table because the contents of the field COL1 is the same as in the first line. The third DELETE statement deletes the third and fourth line from the remaining table because the contents of the default key field COL2 are the same as on the second line. Although the contents of the default key are the same for the first and the fifth line, the fifth line is not deleted because it is not adjacent to the first line.

 

 

 

 

Leaving content frame