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.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get duplicate records by comparing a ztable and a itab

Former Member
0 Kudos

Hi, EXPERTS:

I have an internal table ITAB and a transparent table ZTABLE, with exactly same fields. Before I insert ITAB into ZTABLE, I need to delete those records which are existing with same primary key in ZTABLE, and save these deleted records to another internal table. How to do that?

Thanks.

Tom

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Sort the ITAB based on the key field.

Delete adjacent duplicates comparing <the key fields>.

Ex:

Sort itab by name1.

DELETE ADJACENT DUPLICATES FROM itab comparing name1.

Regards,

Prakash.

6 REPLIES 6

Former Member
0 Kudos

Sort the ITAB based on the key field.

Delete adjacent duplicates comparing <the key fields>.

Ex:

Sort itab by name1.

DELETE ADJACENT DUPLICATES FROM itab comparing name1.

Regards,

Prakash.

Former Member
0 Kudos

U can try the below.

SELECT fields into table idel

from ztable

for all enteries in itab

where f1 = itab-f1 and f2 = itab-f2.

delete ztable from table idel.

Former Member
0 Kudos

Hi,

Create new internal table itab_new where you can keep deleted records & proceed as below.

loop at internal table (ITAB).check data from ZTABLE using select with condition as ztable-field = itab-field.

if data is found then,

move ztable data to new internal table (ITAB_new).

append record .

delete record from ztable.

endif.

endloop.

Thanks.

venkata_ramisetti
Active Contributor
0 Kudos

Hi,

Assume your table is ZTABLE

IATB is where your complete data is stored

ITAB_DEL is the table where you are storing deleted recors

ITAB_ZTABLE is a temp table to check the records in the ZTABLE

DATA: BEGIN OF ITAB_ZTABLE OCCURS 0,

FIELD1 TYPE FIELD1,

FIELD2 TYPE FIELD2,

END OF ITAB_ZTABLE.

SELECT FIELD1 FIELD2 INTO TABLE ITAB_ZTABLE

FROM ZTABLE

FOR ALL ENTRIES IN ITAB

WHERE FIELD1 = ITAB-FIELD1.

LOOP AT ITAB.

READ TABLE ITAB_ZTABLE WITH KEY FIELD1 = ITAB-FIELD1.

IF SY-SUBRC = 0.

APPEND ITAB TO ITAB_DEL.

DELETE ITAB.

ENDIF.

ENDLOOP.

Thanks,

Ramakrishna

Former Member
0 Kudos

Hi,

Try this logic..

SORT ITAB BY KEY FIELDS.

DATA: V_INT TYPE INT4.

LOOP AT ITAB.

AT NEW OF THE LAST KEY FIELD..

CLEAR: V_INT.

ENDAT.

V_INT = V_INT + 1.

IF V_INT > 1.

MOVE ITAB TO ITAB_DUPLICATE.

ENDIF.

AT END OF THE LAST KEY FIELD..

CLEAR V_INT.

ENDAT.

ENDLOOP.

THanks,

Naren

Former Member
0 Kudos

Hi,

follow below logic

data itab like itab occus 0 with header line

if not itab[] ia initial.

select * from ztable into itab1

for all entries in itab

where fld = itab-fld.

if sy-subrc eq 0.

*Some duplicates exists

delete ztable from table itab1.

endif.

*no duplicates exists

insert ztable from table itab.

Regards

amole