Summary -
In this topic, we described about the below sections -
DELETE statement is used to delete one or more records from an internal table. The internal table records can be deleted either by specifying a table key or condition or by finding duplicate entries.
The standard table or sort table with non-unique key can contain the duplicate entries. While deleting the duplicate entries from the above two tables, the first entry from the table is deleted.
Deleting a record or line from the internal table syntax shown below -
DELETE TABLE <itable> [FROM <work_area>/[INDEX <index-num>]].
- <itable> - Specifies the internal table.
- <work-area> - Specifies the work area.
- <index-num> - Specifies the index number of the row/line to delete.
DELETE operation deletes the specific record or line based on the key provided with work area or index number coded with INDEX keyword.
If delete operation is successful, SY-SUBRC set to zero and the indexes of the subsequent lines updated by reducing one. DELETE statement requires an input as either the key in work area or index number. If nothing specified, DELETE operation deletes all the records or lines of the internal table.
Example -
Delete the record having INDEX as 2 from the below internal table data.
Table Data -
Code -
*&---------------------------------------------------------------------*
*& Report Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_INTERNAL_TABLE.
* Specifying table name
TABLES ZTC_TPRODUCT.
* Declaring work area, table it, it1 of type ZTC_TPRODUCT
DATA: WA LIKE ZTC_TPRODUCT,
it TYPE TABLE OF ZTC_TPRODUCT,
it1 TYPE TABLE OF ZTC_TPRODUCT.
* Retrieving data from the table
SELECT *
INTO WA
FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
* Inserting work area to table it.
INSERT WA INTO TABLE it.
ENDSELECT.
* Delete the row by index
DELETE it INDEX 2.
* Displaying header line for output.
WRITE: /'PRODUCTID | PRODUCT '
'| PRODUCT PRICE |'.
ULINE.
* Loop for displaying table it data.
LOOP AT it INTO wa.
* Displaying table data line output.
IF sy-subrc = 0.
WRITE: / WA-PRODUCTID, '|', WA-PRODUCT, '|',WA-PRODUCT_PRICE, '|'.
ELSE.
WRITE 'No Record Found'.
ENDIF.
ENDLOOP.
ULINE.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
Work area(WA) defined like ZTC_TPRODUCT table structure. Internal table(it) defined as type of ZTC_TPRODUCT table.
DELETE statement deletes the record that is having index 2. So the PRODUCTID as IFB2 record got deleted from the internal table.
DELETE with KEY -
DELETE statement deletes the record or line that matches to the specified internal table key. DELETE statement with KEY syntax is specified below -
DELETE TABLE <itable> WITH TABLE KEY
<K1> = <F1>………… <Kn> = <Fn>.
- <itable> - Specifies the internal table.
- <F1>, <F2>....<Fn> - Specifies the fields of an internal table.
- <K1>, <K2>....<Kn> - Specifies key fields of the table.
The DELETE statement used to delete the records or lines of the <itable> table based on the expressions <K1> = <F1>, <K2> = <F2>...<Kn> = <Fn>.
DELETE with WHERE -
DELETE statement with WHERE clause deletes the single or multiple lines. DELETE statement with WHERE clause deletes all the lines that are satisfied with the specified logical condition.
DELETE <itable> [FROM <n1>]
[TO <n2>] [WHERE <condition>].
- <itable> - Specifies the internal table.
- FROM <n1> - Specifies the index of the first line to be delete.
- TO <n2> - Specifies the index of the last line to be delete.
- WHERE <condition> - Specifies the first operand of the logical expression must be a component of <table>.
DELETE statement deletes the rows/lines in between the index n1 to index n2 that satisfies the logical expression. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.
Example -
Delete the records of LG product from the below internal table data.
Table Data -
Code -
*&---------------------------------------------------------------------*
*& Report Z_INTERNAL_TABLE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_INTERNAL_TABLE.
* Specifying table name
TABLES ZTC_TPRODUCT.
* Declaring work area, table it of type ZTC_TPRODUCT
DATA: WA LIKE ZTC_TPRODUCT,
it TYPE TABLE OF ZTC_TPRODUCT.
* Retrieving data from the table
SELECT *
INTO WA
FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
* Inserting work area to table it.
INSERT WA INTO TABLE it.
ENDSELECT.
* Delete the row WHERE the PRODUCTID is LG3
DELETE it WHERE PRODUCTID = 'LG3'.
* Displaying header line for output.
WRITE: /'PRODUCTID | PRODUCT '
'| PAMOUNT |'.
ULINE.
* Loop for displaying table it data.
LOOP AT it INTO wa.
* Displaying table data line output.
IF sy-subrc = 0.
WRITE: / WA-PRODUCTID, '|', WA-PRODUCT, '|',WA-PAMOUNT, '|'.
ELSE.
WRITE 'No Record Found'.
ENDIF.
ENDLOOP.
ULINE.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
Work area(WA) defined like ZTC_TPRODUCT table structure. Internal table(it) defined as type of ZTC_TPRODUCT table.
DELETE statement deletes the record with the PRODUCTID as LG3. So the PRODUCTID as LG3 record got deleted from the internal table.