2011 Jan 18 10:22 AM
Hello All,
In my program i have to delete duplicate records from dynamic table.
I tried using DELETE ADJACENT DUPLICATES FROM <DYN_TABLE> COMPARING <fieldname1> <fieldname2> but it fails in syntax check, then i tried like below:
DATA string type string.
string = '<fieldname1> <fieldname2>.........'.
DELETE ADJACENT DUPLICATES FROM <DYN_TABLE> COMPARING (string).
It also got failed at runtime.
Please suggest how can i achieve this....
Regards
Munish Garg
2011 Jan 18 10:31 AM
Hi,
Did you the sort internal table before doing the delete adjcent duplicates. Otherwise the delete adjacent duplicatent statment will fails.
With Regards,
Sumodh.P
2011 Jan 18 10:33 AM
Hi Sumodh,
Ofcourse i sorted it out first. And my problem is with syntax not with result. I have posted the thread to acheive the syntax free statement.
Regards
Munish Garg
2011 Jan 18 10:37 AM
Hi,
Use
DELTE ADJACENT DUPLCIATES from <itab> comparing all fields.
2011 Jan 18 10:39 AM
I have to delete based upon few fields not all so i have to supply field names...
2011 Jan 18 10:51 AM
Check this sample code.
data: tab(10) type c.
PARAMETERS: p_tab like tab.
FIELD-SYMBOLS: <fs> type STANDARD TABLE.
data: itab type ref to data,
field TYPE string.
field = 'AENAM'.
CREATE DATA: itab TYPE STANDARD TABLE OF (p_tab).
ASSIGN itab->* to <fs>.
select * from (p_tab) into TABLE <fs> UP TO 10 ROWS.
sort <fs> by (field).
delete ADJACENT DUPLICATES FROM <fs> COMPARING (field).
PS: If you are unsure of the field name on which you have to compare, use the ASSIGN COMPONENT syntax and get the reference of the field
Vikranth
2011 Jan 18 11:26 AM
Hi vikrant,
THe sample code worked but my fieldnames in dynamic internal table are MARD-MATNR MARD-WERKS MARD-LGORT. I want to sort it with MARD-MATNR and MARD-WERKS.
If i use the same code to do that then it gives me dump at SORT itself however SORT can be done by using OTAB concept but DELETE ADJACENT DUPLICATES is not happening.
????
Regards
Munish Garg
2011 Jan 18 11:57 AM
2011 Jan 18 12:03 PM
Hi Vikrant,
I have <DYN_TABLE> having 3 columns with exact name as MARD-MATNR, MARD-WERKS and MARD-LGORT.
Just see the code i am trying(with values hardcoded):
DATA: otab TYPE abap_sortorder_tab,
oline TYPE abap_sortorder,
string TYPE string.
oline-name = 'MARD-MATNR'.
APPEND OLINE TO OTAB.
oline-name = 'MARD-WERKS'.
APPEND OLINE TO OTAB.
SORT <DYN_TABLE> BY (OTAB).
string = 'MARD-MATNR MARD-WERKS'.
DELETE ADJACENT DUPLICATES FROM <DYN_TABLE> COMPARING (string).
It gives me dump at last statement.
Regards
Munish Garg
2011 Jan 18 12:41 PM
Hello,
Dynamic tokens are allowed with SORT & D.A.D: -
SORT <DYN_TABLE> BY ('MARD-MATNR') ('MARD-WERKS').
DELETE ADJACENT DUPLICATES FROM <DYN_TABLE> COMPARING ('MARD-MATNR') ('MARD-WERKS').
BR,
Suhas
2011 Jan 18 1:05 PM
Hi Suhas,
It was good one but as it is dynamic table and number of fields will change in next iteration so I cant hardcode the values.
Please let me know how can we acheive this dynamically.
Regards
Munish Garg
2011 Jan 18 3:24 PM
Hi,
Are the fields always from a table?
why don´t you use the FM - DDIF_FIELDINFO_GET.
You just want to compare the 2 keys from that table but i don´t know if in the next iteraction you want 3 keys.
Having all the fields it should be more or less easy to build your string and then use the delete.
2011 Jan 19 4:40 AM
Hello Munish,
... it is dynamic table and number of fields will change in next iteration so I cant hardcode the values.
How do you determine based on which fields you want to D.A.D from the internal table?
BR,
Suhas
2011 Jan 19 4:44 AM
Hi Suhas,
I have another internal table which stores this information. Suppose that internal table is having 2 fields then i have to do D.A.D. for those 2 fields, in case 3 then do D.A.D with 3 fields....and so on.....
Regards
Munish Garg
2011 Jan 19 5:04 AM
Hello Munish,
If this is the case i would suggest try out Clemen's code involving SORTED TABLES WITH UNIQUE KEY!
BR,
Suhas
2011 Jan 18 5:19 PM
Hi Munish,
let me throw in an idea to circumvent the syntax questions:
I hope you create internal table dynamically using CREATE DATA statement.
CREATE DATA dref
TYPE SORTED TABLE
OF (name)
WITH UNIQUE KEY (keytab).
This way you put the COMPARING fields into key and will never have duplicates.
Even if you need the table as standard table for reasons beyond the scope of this topic, you could create a generic form/method doing this.
You pass the table as changing parameter and a table of sort/compare fields and then
CREATE DATA dref
LIKE LINE OF pt_itab.
ASSIGN dref->* to <any>.
CREATE DATA dref
LIKE SORTED TABLE
OF <any>
WITH UNIQUE KEY (pt_keytab).
ASSIGN dref->* to <tab>.
LOOP AT pt_itab ASSIGNING <any>.
INSERT <any> INTO TABLE <tab>.
ENDLOOP.
pt_itab = <tab>.
Just an idea (but I like it)
Regards,
Clemens
2011 Jan 18 6:38 PM
Hi,
Please check the below sample code. I think you will get your answer.
DATA: l_data TYPE REF TO data,
l_field TYPE string.
FIELD-SYMBOLS: <tab> TYPE STANDARD TABLE.
CREATE DATA l_data TYPE STANDARD TABLE OF vbap.
ASSIGN l_data->* TO <tab>.
SELECT * FROM vbap
UP TO 100 ROWS
INTO TABLE <tab>.
l_field = 'MATNR'.
SORT <tab> BY (l_field). " While you building Dyn table you know the field names
DELETE ADJACENT DUPLICATES FROM <tab> COMPARING (l_field).
IF sy-subrc = 0.
ENDIF.
CREATE DATA l_data TYPE STANDARD TABLE OF vbrp.
ASSIGN l_data->* TO <tab>.
SELECT * FROM vbrp
UP TO 1000 ROWS
INTO TABLE <tab>.
l_field = 'POSNR'.
SORT <tab> BY (l_field).
DELETE ADJACENT DUPLICATES FROM <tab> COMPARING (l_field).
IF sy-subrc = 0.
ENDIF.
Thanks
Subhankar
2011 Jan 20 9:16 AM
Hi munish,
For dynamic table there is no predefined structure thats why you can not specify field in the statement. If want to restrict it
to some field you can pass it only as tokens but next time it may be different so you can not also do that. you can only use
comparing all fields other wise it will give run time error.
you can only sort your internal table based upon the keys you want and delete adjacent dupticate in field symbol.
2011 Jan 20 9:45 AM
2011 Jan 21 5:47 PM
Hi Munish,
Why dont you try this
fld1 = sort_field1.
fld2 = sort_field2.
....
....
sort <it_data> by (fld1) (fld2) .....
delete adjecent duplicate from <it_data> comparing (fld1) (fld2) (fld3).....
2011 Feb 10 6:58 AM
2013 Oct 04 6:35 AM
Hi,
Me too face the same problem in delete adjacent this code :
SORT gt_object_item1 by qmnum.
DELETE ADJACENT DUPLICATES FROM gt_object_item1 COMPARING ALL FIELDS.
Can any body suggest what is happening here. Even when all fields are same some same entries remain undeleted. Its urgent hope for fast response!!!
Thnks!!
2014 Mar 21 10:02 AM
Hi,
this code is worked truly.
You can try this.
Regards.
DATA itab LIKE STANDARD TABLE OF mard.
SELECT * FROM mard INTO TABLE itab UP TO 100 ROWS.
* You define max number of the field that you need
data : cmp01 TYPE string,
cmp02 TYPE string,
cmp03 TYPE string,
cmp04 TYPE string,
cmp05 TYPE string.
* You fill the fields that you need, others is clear.
cmp01 = 'MATNR'.
cmp02 = 'WERKS'.
CLEAR : cmp03,cmp04,cmp05.
SORT itab BY (cmp01) (cmp02) (cmp03) (cmp04) (cmp05).
DELETE ADJACENT DUPLICATES FROM itab COMPARING (cmp01) (cmp02) (cmp03)
(cmp04) (cmp05).
2014 Mar 21 10:59 AM
Hi,
this code is worked truly.
You can try this.
Regards.
DATA itab LIKE STANDARD TABLE OF mard.
SELECT * FROM mard INTO TABLE itab UP TO 100 ROWS.
* You define max number of the field that you need
data : cmp01 TYPE string,
cmp02 TYPE string,
cmp03 TYPE string,
cmp04 TYPE string,
cmp05 TYPE string.
* You fill the fields that you need, others is clear.
cmp01 = 'MATNR'.
cmp02 = 'WERKS'.
CLEAR : cmp03,cmp04,cmp05.
SORT itab BY (cmp01) (cmp02) (cmp03) (cmp04) (cmp05).
DELETE ADJACENT DUPLICATES FROM itab COMPARING (cmp01) (cmp02) (cmp03)
(cmp04) (cmp05).
2016 Apr 15 1:49 PM
Hi, Munish
Before using DELETE ADJACENT DUPLICATES statement, you have to sort the table by ascending.
like
sort <DYN_TABLE> ascending by <fieldname1>.
DELETE ADJACENT DUPLICATES FROM <DYN_TABLE> COMPARING <fieldname1> <fieldname2>.
Regards,
Suman