Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
Showing results for 
Search instead for 
Did you mean: 

Delete adjacent duplicates : Dynamic comparing

Former Member
0 Kudos


I want to trigger this kind of abap line code:

delete adjacent duplicates from my_itab comparing (lv_compare).

But it does not work.

Where in lv_compare, i have many fieds.

Before executing, I don't know neither the number of field nor names


Active Contributor
0 Kudos

When ABAP syntax does not offer dynamic options, like in this case, the last resort is to temporarily generate and execute code using the GENERATE SUBROUTINE POOL statement, see ABAP online help.

However using this statement can be subject to checks and questions by IT auditors, just so that you know


0 Kudos

Thank you Thomas for your answer.

Former Member
0 Kudos


I have done dynamic comparision for delete adjacent duplicates for dynamic internal table, using the key fields of the table. Please find the sample code below. The maximum number of key fields is 7 for the tables that i will be accessing. So have used 7 fieldsymbols in my 'delete adjacent duplicate'.

<removed by moderator>

DATA : lv_index             TYPE numc2.
DATA : lv_field_01          TYPE fieldname.
DATA : lv_field_02          TYPE fieldname.
DATA : lv_field_03          TYPE fieldname.
DATA : lv_field_04          TYPE fieldname.
DATA : lv_field_05          TYPE fieldname.
DATA : lv_field_06          TYPE fieldname.
DATA : lv_field_07          TYPE fieldname.
DATA : lv_key_fieldname     TYPE char50.
DATA : lt_keyfields         TYPE abap_sortorder_tab.
DATA : lwa_keyfields        TYPE abap_sortorder.

FIELD-SYMBOLS : <lv_key_fieldname>    TYPE ANY.

* Get the key fields of the table
SELECT fieldname FROM dd03l
INTO TABLE lt_tabkfields
WHERE tabname = pv_tabname
AND   keyflag = 'X'.

LOOP AT lt_tabkfields INTO lwa_tabkfields.
  lv_index = lv_index + 1.

  lwa_keyfields-name = lwa_intlen-fieldname.
  APPEND lwa_keyfields TO lt_keyfields.

  CONCATENATE 'LV_FIELD' lv_index INTO lv_key_fieldname SEPARATED BY '_'.
  ASSIGN (lv_key_fieldname) TO <lv_key_fieldname>.
  IF sy-subrc EQ 0.
    <lv_key_fieldname> = lwa_tabkfields-fieldname.

SORT <lfs_xdata_table> BY (lt_keyfields).
  COMPARING (lv_field_01) (lv_field_02) (lv_field_03)
            (lv_field_04) (lv_field_05) (lv_field_06) (lv_field_07).



Edited by: Thomas Zloch on Mar 2, 2012

0 Kudos

Dear Priya,

I have already try it but I was not really satisfied by this solution.

I'm sure it will probably work because I don't expect to have more than 7 fields to sort. but we never know...



0 Kudos

what is the structure of lv_compare?

At runtime how this is filled with name of fields? an example will be helpfull to understand the requiremnt.

0 Kudos


If it is feasible, you can give the names of all the fields in the Delete Adjacent statement.

DELETE ADJACENT DUPLICATES FROM i_table COMPARING (l_var1) (l_var2) (l_var3) (l_var4).......

At runtime you can assign values to these variables. If a variable is empty then it is ignored otherwise it will be considered.


Harsh Bansal