SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow


How to Delete from Internal Table using ABAP

As an ABAP developer, I frequently require to delete an entry from an internal table using ABAP codes.
I believe this is a common task and many developers requires simple ABAP commands to delete from internal table, or remove entries from an ABAP internal table.

Here is the two methods I frequently use to delete from internal table.

The below ABAP code block deletes entries from internal table gt_result where field langu has a different value other than p_langu selection parameter has.

DELETE gt_result WHERE langu NE p_langu.
Code

Using the following ABAP script, developers can also delete or remove unwanted rows from an internal table.
But the following DELETE structure has a loop mechanism.
By looping in the internal table entries, the ABAP code checks a certain condition. If the condition occurs then the DELETE ABAP command removes the current loop item from internal table.
The key point in this code is using "INDEX sy-tabix" hint which points to the index of the current loop item.

LOOP AT gt_result INTO gwa_search.
  IF gwa_search-sysid NE p_sapsys.
    DELETE gt_result INDEX sy-tabix.
  ENDIF.
ENDLOOP.
Code

Following ABAP code block populates an internal table with sample data. After then using an ABAP Loop command, each internal table row is processed. In case the internal table text is not matching a criteria, the internal table row is deleted using the "Delete itable Index sy-tabix" ABAP command.

delete internal table row in Loop command using ABAP

DATA lt_notes TYPE TABLE OF string.
DATA ls_notes TYPE string.

ls_notes = 'TextId:2783001-11'.
APPEND ls_notes TO lt_notes.
ls_notes = 'Text to be deleted'.
APPEND ls_notes TO lt_notes.
ls_notes = 'TextId:2783003-13'.
APPEND ls_notes TO lt_notes.

LOOP AT lt_notes INTO ls_notes.

 REPLACE 'TextId:' WITH 'Metin:' INTO ls_notes.
 IF sy-subrc = 0.
  MODIFY lt_notes FROM ls_notes.
 ELSE.
  DELETE lt_notes INDEX sy-tabix.
 ENDIF.

ENDLOOP.
Code

Here is the ABAP code illustrated in above screenshot where you can use to delete internal table row within a Loop command.



SAP HANA and ABAP

Install SAP Free
CRM Companies List
Web Based CRM Software


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.