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

You can use the DELETE statement to delete one or more lines from tables using their 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-SUBRC 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 loop 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 <itab> [FROM <n1>] [TO <n 2>] [WHERE <condition>].

Here, you must specify at least one of the additions. The WHERE addition has the same effect as when you delete entries from any table. As well as the WHERE clause, 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 <n 1 > and <n 2 >. 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.

Examples

Example

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

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

         1        28        784

         2        29        841

         3        30        900

The example fills a sorted table ITAB 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

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

 

 

 

Leaving content frame