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

Delete adjacent duplicates from a dynamic table

Former Member
0 Likes
1,564

Hi,

How can I delete adjacent duplicates from a dynamic table using the table keys.

I have concatenated the table keys to a string variable.

When I try to delete the duplicates I receive a dump.

Does anyone knows a way to overcome it ?

Thanks.

Hi,

How can I delete adjacent duplicates from a dynamic table using the table keys.

I have concatenated the table keys to a string variable.

When I try to delete the duplicates I receive a dump.

Does anyone knows a way to overcome it ?

Thanks.

4 REPLIES 4
Read only

andreas_mann3
Active Contributor
0 Likes
1,140

try to use :

Delete adjacent duplicates from itab COMPARING ALL FIELDS

A.

Read only

daixiong_jiang3
Active Participant
0 Likes
1,140

you can use a function GET_COMPONENT_LIST to get all the fields' name after the dynamic table generated. then you can use the field name in the statement "DELETE....COMPARE "

CALL FUNCTION 'GET_COMPONENT_LIST'

EXPORTING

program = SY-REPID

fieldname = THE_NAME_OF_THE_DYNAMIC_INTERNAL_TABLE

tables

components = ITAB

.

LOOP AT ITAB.

*ASSIGN THE ITAB-COMPNAME TO SOME VARIANTS:FIELD1,FIELD2,FIELD3.

ENDLOOP.

SORT THE_NAME_OF_THE_DYNAMIC_INTERNAL_TABLE BY FIELD1 FIELD2 FIELD3.

DELETE ADJACENT DUPLICATES FROM THE_NAME_OF_THE_DYNAMIC_INTERNAL_TABLE COMPARING FIELD1 FIELD2 FIELD3.

Read only

Former Member
0 Likes
1,140

I wont delete the duplicates depending on the table keys - and I don't know the number of keys.

Read only

dirk_schiebeler
Associate
Associate
0 Likes
1,140

Try this approach:

DATA:
  tabname TYPE tabname, "db table name from DDIC
  lt_key_fields TYPE abap_sortorder_tab,
  lv_key1 TYPE fieldname,
  lv_key2 TYPE fieldname,
  lv_key3 TYPE fieldname,
  lv_key4 TYPE fieldname,
  lv_key5 TYPE fieldname,
  lo_tabletype TYPE REF TO data,
  lo_table_descr TYPE REF TO cl_abap_tabledescr.

FIELD-SYMBOLS: <lt_cust_data> TYPE STANDARD TABLE.

CREATE DATA lo_tabletype TYPE STANDARD TABLE OF (tabname).
lo_table_descr ?= cl_abap_tabledescr=>describe_by_data_ref( p_data_ref = lo_tabletype ).
DATA(lt_keys) = lo_table_descr->get_keys( ).
lt_key_fields = CORRESPONDING #( lt_keys[ 1 ]-components ). "sorted ascending, not as TEXT

CLEAR: lv_key1, lv_key2, lv_key3, lv_key4, lv_key5.
LOOP AT lt_key_fields ASSIGNING FIELD-SYMBOL(<ls_field>).
  IF <ls_field>-name NE 'MANDT' AND "Skip the client field!
    <ls_field>-name NE 'CLIENT'.
    IF lv_key1 IS INITIAL.
      lv_key1 = <ls_field>-name.
    ELSEIF lv_key2 IS INITIAL.
      lv_key2 = <ls_field>-name.
    ELSEIF lv_key3 IS INITIAL.
      lv_key3 = <ls_field>-name.
    ELSEIF lv_key4 IS INITIAL.
      lv_key4 = <ls_field>-name.
    ELSEIF lv_key5 IS INITIAL.
      lv_key5 = <ls_field>-name.
    ENDIF.
  ENDIF.
ENDLOOP.

ASSIGN <ls_cust_reg>-data->* TO <lt_cust_data>. "that is a standard table of "tabname"
APPEND LINES OF <lt_insertable_data> TO <lt_cust_data>. "that are some new table entries...
SORT <lt_cust_data> STABLE BY (lt_key_fields).
DELETE ADJACENT DUPLICATES FROM <lt_cust_data> COMPARING (lv_key1) (lv_key2) (lv_key3) (lv_key4) (lv_key5).