Skip to Content
Technical Articles
Author's profile photo Huzefa Saifee

Deleting records off an undefined internal table

It is useful to have a generic method to remove records off any internal table based on a certain field value, even when you don’t know the table structure at design time. The end result of this code would be a method call with a changing parameter in which you feed your internal table:

In this example, we’re using PERNR as the field by which the filtering decision is made.

CLASS main DEFINITION.

  PUBLIC SECTION.
    METHODS: suppression_check IMPORTING iv_pernr TYPE persno
                              RETURNING value(rv_skipflag) TYPE flag,
             filter_table_values CHANGING ct_values TYPE ANY TABLE.

ENDCLASS.                    "main DEFINITION

CLASS main IMPLEMENTATION.

  METHOD suppression_check.
    "logic to check if the record must be suppressed or not
    "using the field value and returning the skip flag
  ENDMETHOD.                    "suppression_check

  METHOD filter_table_values.

    FIELD-SYMBOLS: <fs_record> TYPE ANY,
                   <fs_pernr> TYPE ANY.

    LOOP AT ct_values ASSIGNING <fs_record>.
      ASSIGN COMPONENT 'PERNR' OF STRUCTURE <fs_record> TO <fs_pernr>.
      IF me->suppression_check( <fs_pernr> ).
        DELETE TABLE ct_values[] FROM <fs_record>.
      ENDIF.
    ENDLOOP.

  ENDMETHOD.
ENDCLASS.

While using this in the program:

lo->filter_table_values( lt_table ).

I hope this helps, and please do suggest improvements.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Tomas Buryanek
      Tomas Buryanek

      Here is another approach using dynamic DELETE itab WHERE :

      * <SIGNATURE>---------------------------------------------------------------------------------------+
      * | [--->] IV_FIELDNAME                   TYPE        STRING
      * | [--->] IT_FILTER                      TYPE        ANY TABLE
      * | [<-->] CT_TABLE                       TYPE        ANY TABLE
      * +--------------------------------------------------------------------------------------</SIGNATURE>
        METHOD filter_table.
      
          DATA(lv_where_syntax) = |{ iv_fieldname } IN IT_FILTER|.
          DELETE ct_table WHERE (lv_where_syntax).
      
        ENDMETHOD.

      IT_FILTER should have range table structure (SIGN, OPTION, LOW, HIGH fields).

      EDIT: if someone tries this, please read also ABAP help and add error handling. There can be CX_SY_ITAB_DYN_LOOP exception thrown for example...

      Author's profile photo Huzefa Saifee
      Huzefa Saifee
      Blog Post Author

      Thanks for this! This rids us of the loop I used.

      I wanted a stand-alone method to give me a yes/no answer on whether the record needs to be suppressed, which is why I wrote the code as above. I'm working on SAP HR and I use this to REJECT records in a virtual database report after GET PERNR and END-OF-SELECTION, which is why the module had to be the way it is.

      While your code would involve compiling the IT_FILTER table, I think it would be worth it performance wise.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      It would be a valuable insight to mention the context in the blog text. SAP HR has some old-timey stuff that imposes some limitations, so different code could make more sense in that specific case.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Can you update the blog title to make it clear you're talking about an undefined internal table.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      I saw the title (still unchanged as of now) and was like "what in tarnation..." %-[    ]

      I guess it's a good tactic to drive traffic to the post, LOL.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Thanks for sharing!

      What happens if ct_values does not have a field named PERNR?

      What if it's not a standard table but sorted or hashed?

      What if it has a million records?

      Could this task also be accomplished without lv_pernr?

      I think these questions are worth exploring (and would likely come up in a code review).

      P.S. It's "suppression", double "p". There are some neat tools in ADT these days, including spell check. I'd go with a different name though, the one that sounds more natural with the functional call and is less prone to typos. Like skip_record.

      Author's profile photo Huzefa Saifee
      Huzefa Saifee
      Blog Post Author

      Thanks for your comment Jelena! Tomas Buryanek's code in the comments could work for all types of internal tables and would be more robust for a million records.

      I made the sin of declaring a variable when none was required. I have rectified that.

      If there is no PERNR, there would be a dump. But I didn't want to put error handline code over here.

      As for the extra 'p', I guess I suppressed it by mistake. 😀