How to delete records from a Database table in SAP ABAP

Using the DELETE statement, you can delete one or more records from a Database table. It can be deleted in the following three different ways.

  • Through ‘WHERE’ clause.
  • Through structure (work area).
  • Through Internal table.

  • Through ‘WHERE’ clause.

The data records from the Database table are deleted based on the condition mentioned in the ‘WHERE’ clause. If at least one record is deleted from the Database table, then the system field is set to sy-subrc = 0. If the deletion is unsuccessful, then system field sy-subrc is set to 4. The system field sy-dbcnt will give out the count of the records deleted.

DELETE FROM ztt_db_table2 WHERE contact_id = 102.

  • Through Structure (Work area).

With the help of the structure, we can delete exactly a single line of record from the Database table. The keyword ‘FROM’ will be used after the table name. If the single line of record is found and deleted, then system field sy-subrc will be set to 0. If the record is not found and therefore not deleted, then the system field sy-subrc will be set to 4.

DATA ls_contact TYPE ztt_db_table2.

ls_contact – contact_id = 101.

ls_contact – contact_name = ‘Thiru’.

ls_contact – contact_name = ‘Street 10’.

DELETE ztt_db_table2 FROM ls_contact.

  • Through Internal table.

The Data records from the Database table can also be deleted through Internal table. Initially, the internal table is loaded with all the data records to be deleted. ‘FROM’ keyword is used after the Database table name and then it is followed by ‘TABLE’ keyword. The internal table name is mentioned after the ‘TABLE’ keyword.

DATA lt_int_table TYPE TABLE OF ztt_db_table2.

DATA ls_data_record TYPE ztt_db_table2.

ls_data_record – contact_id = 103.

ls_data_record – contact_name = ‘Mahesh’.

ls_data_record – contact_address = ‘Street 10’.

APPEND ls_data_record TO lt_int_table.

ls_data_record – contact_id = 102.

ls_data_record – contact_name = ‘Mani’.

ls_data_record – contact_address = ‘Street 11’.

APPEND ls_data_record TO lt_int_table.

DELETE ztt_db_table2 FROM TABLE lt_int_table.