Start of Content Area

Deleting Lines  Locate the document in its SAP Library structure

To delete a single line of any internal table, use the DELETEstatement. 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 = f1 ... kn = fn.

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. To delete tables with the elementary line type and the table_line key using the table keys, you can use either the first variant or the table_line pseudo component  as an operand in the second variant.

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 dynamically as the content of a field k1 k2 using (k1) = f1 (k2) = f2  If the data types of f1 fn are not compatible with the key fields, the system converts them.

The system searches for the relevant table types as follows:

·        Standard tables

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

·        Sorted tables

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

·        Hashed tables

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 expression cond. The logical expression cond 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.

If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as comparison values using comp->attr or table_line->attr.

Deleting Adjacent Duplicate Entries

To delete adjacent duplicate entries use the following statement:

DELETE ADJACENT DUPLICATE ENTRIES FROM itab
                                 [COMPARING f1 f2 ... |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:

·        Without the COMPARINGaddition, the contents of the key fields of the table must be identical in both lines.

·        If you use the addition COMPARING f1 f2 ... the contents of the specified fields f1 f2 ... must be identical in both lines. You can also specify the fields f1 f2… dynamically using (n1) (n2) … as the contents of a field n1 n2. If n1 n2 is empty when the statement is executed, it is ignored. You can also restrict all fields f1 f2 … to subfields by specifying offset and length.

·        If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.

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.

Example

REPORT demo_int_tables_DELETE_from .

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 list output is:

         2        4
         4       16

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

Example

REPORT demo_int_tables_DELETE_where.

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 list 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

REPORT demo_int_tables_DELETE_adjacen.

DATA off TYPE i.

DATA: BEGIN OF line,
        col1    TYPE i,
        col2(1) 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 list 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, as 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.

Deleting Lines Using the Index

You can use the DELETE statement to delete lines in tables using their index. You can insert one or several lines into internal tables via the index:

Deleting a Single Line

To delete a line using its index, use the following statement:

DELETE itab [INDEX idx].

If you use the INDEX addition, the system deletes the line with the index idx from table itab, reduces the index of the subsequent lines by 1, and sets sy-subrc to zero. Otherwise, if no line with index idx exists, sy-bubrc is set to 4.

Without the INDEX addition, you can only use the above statement within a LOOP. In this case, you delete the current line. idx is implicitly set to sy-tabix .

Deleting Several Lines

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

DELETE [FROM n1] [TO n2] [WHERE condition].

Here, you must specify at least one of the additions. The WHERE addition work in the same way as any other tables. As well as the WHEREaddition, you can specify the lines that you want to delete by their index using FROM and TO. The system deletes all of the lines of itab whose index lies between n1 und n2. If you do not specify a FROM addition, the system deletes lines from the first line onwards. If you do not specify a TO addition, the system deletes lines up to the last line.

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

Example

REPORT demo_int_tables_delete_ind_1.

DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.

DO 5 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.

ENDDO.

DELETE itab INDEX: 2, 3, 4.

WRITE: 'sy-subrc =',sy-subrc.

SKIP.

LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.

The list output is:

sy-subrc     4

         1          1          1
         2          3          9
         3          5         25

The example fills a sorted table itab with five lines. Then it deletes the three lines with the indexes 2, 3, and 4. After deleting the line with index 2, the index of the following lines is decremented by one. Therefore, the next deletion removes the line with an index which was initially 4. The third delete operation fails, since the table now only has three lines.

Example

REPORT demo_int_tables_delete_ind_2.

DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.

DATA itab LIKE TABLE OF line.

DO 30 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.

ENDDO.

LOOP AT itab INTO line.
  IF line-col1 < 28.
    DELETE itab.

  ENDIF.
ENDLOOP.

LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.

The list output is:

         1        28        784

         2        29        841

         3        30        900

A standard table ITAB is filled with 30 lines. Using a LOOP construction, the program deletes all of the lines in the table with a value of less than 28 in field col1.

Example

REPORT demo_int_tables_delete_ind_3.

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE TABLE OF line.

DO 40 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.

ENDDO.

DELETE itab FROM 3 TO 38 WHERE col2 > 20.

LOOP AT itab INTO line.
  WRITE: / line-col1, line-col2.
ENDLOOP.

The list output is:

         1          1

         2          4

         3          9

         4         16

        39      1.521

        40      1.600

The program deletes all entries from the standard table itab with an index between 3 and 39 where the value in col2 is greater than 20.

 

 

End of Content Area