How to Delete records from the internal table in SAP ABAP

The Data records present in the internal table can be deleted in following ways.

  • Key or work area (structure)
  • WHERE conditions.
  • Index
  • From Index TO Index
  • Clean, Refresh, free

DATA lt_internal_table TYPE TABLE OF ztt_db_table2.

DATA ls_structure TYPE ztt_db_table2.

SELECT *FROM ztt_db_Table2 INTO TABLE lt_internal_table.

              ls_structure – contact_id = 102.

              ls_structure – contact_name = ‘Thiru’.

              ls_structure – contact_address = ‘Street 10’.

Key or Work Area:

DELETE TABLE lt_internal_Table FROM ls_structure.

Only for key or structure, ‘TABLE’ is written explicitly after DELETE in the syntax.

WHERE:

DELETE lt_internal_table WHERE contact_id = 102.

INDEX:

DELETE lt_internal_table INDEX 2.

FROM INDEX TO INDEX:

DELETE lt_internal_table FROM 2 TO 3.

If the deletion is successful, then the system field sy-subrc will hold the value 0. If it is not successful, it will hold the value 4.

How to declare an internal table in three different ways

DECLARATION OF INTERNAL TABLE OF TYPE (DATABASE TABLE)

DATA It_internal_table TYPE STANDARD TABLE OF ztt_db_table1.

DATA Is_structure LIKE LINE OF It_internal_table.

DO’s

  • DATA Is_structure TYPE ztt_db_table1

DON’TS

  • One common mistake observed from beginners: please declare the internal type explicitly like below ‘TYPE STANDARD TABLE OF’. Otherwise it will consider as structure.

DATA ls_struct_instead_of_internal_table TYPE ztt_db_table1.

  • DATA Is_structure TYPE lt_internal_table. if it is written like this, it will show compilation error –“Type LT_INTERNAL TABLE’ is unknown”.
  • DATA Is_structure LIKE It_internal_table. Here Is_structure is considered as internal table. It shows error which assigning values to the structure. The compilation error such as ‘Is structure’ is a table without a header line and therefore does not have a component called “FIELD1”, ‘FIELD2′,’FIELD3’. It also shows compilation error in the line ‘INSERT INTO ztt_db_table1 VALUES Is_structure’ saying that internal tables cannot be used as work areas.

“CHECKING INTERNAL TABLE OF TYPE (DATABASE TABLE)

Is structure-field1 = 10.

Is structure-field2 = ‘Ten’.

Is structure-fieid3 = ‘Number: 10’.

INSERT INTO ztt_db_table1 VALUES Is_structure.

SELECT * FROM ztt_db_table1 INTO TABLE It_internal_table.

LOOP AT It_internal_table ASSIGNING FIELD-SYMBOL(<Is_internal_table>).

WRITE:/ <ls_internaltable>-field1.

WRITE:/ <Is_internal_table›-field2.

WRITE:/ <Is_internal_table>-field3.

ENDLOOP.

DECLARATION OF LOCAL INTERNAL TABLE AND STRUCTURE

TYPES: BEGIN OF st_structure,

              Field1 TYPE zdel_id,

              Field2 TYPE zdel_name,

              Field3 TYPE zdel_desc,

           END OF st_structure.

TYPES: tt_internal_table TYPE STANDARD TABLE OF st_structure.

DATA pt_internal_table TYPE tt_internal_table.

DATA ls_local_structure LIKE LINE OF pt_internal_table.

“ CHECKING LOCAL INTERNAL TABLE AND STRUCTURE

Ls_local_structure-field1 = 20.

Ls_local_structure-field2 = ‘Twenty’.

Ls_local_structure-field3 = ‘Number: 20’.

INSERT INTO ztt_db_table1 VALUES ls_local_structure.

SELECT * FROM ztt_db_table1 INTO TABLE pt_internal_table.

LOOP AT pt_internal_table ASSIGNING <ls_internal_table>.

              WRITE:/ <ls_internal_table>-field1.

              WRITE:/ <ls_internal_table>-field2.

              WRITE:/ <ls_internal_table>-field3.

 ENDLOOP.

DO’S

  • DATA ls_local_structure TYPE st_structure. Instead of using the LIKE LINE OF internal_table. We can directly specify the structure type.

DON’TS

  • St_structure-field1 = 20. Please don’t use variables of ‘TYPES’. We need to explicitly define a variable in order to use it. It shows the compilation error “Field is unknown”. The same applies for the table type, so here ‘SELECT * FROM ztt_db_table1 INTO TABLE tt_internal_table.’

tt_internal_table is just a datatype. We need to explicitly declare a variable of this type, in order to use it.

DECLARATION OF INTERNAL TABLE OF TYPE ( TABLE TYPE)

DATA lt_int_table_of_tt TYPE ztt_table_type.

DATA ls_structure_of_tt TYPE zss_structure.

“CHECKING LOCAL INTERNAL TABLE AND STRUCTURE

Ls_structure_of_tt-m_field4 = 30.

Ls_structure_of_tt-m_field5 = 30.

APPEND ls_structure_of_tt TO lt_int_table_of_tt.

LOOP AT lt_int_table_of_tt ASSIGNING FIELD-SYMBOL(<ls_structure_of_tt>).

              WRITE:/ <ls_structure_of_tt>-m_field4.

              WRITE:/ <ls_structure_of_tt>-m_field5.

ENDLOOP.