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: 

To compare two internal tables and delete records

Former Member
0 Kudos
7,058

Hi friends,

I have to compare two internal tables and should delete the records which is not present in both the tables. Reply me as soon as possible.

Thanks.

1 ACCEPTED SOLUTION

Former Member
763

Hi Nag,

If you have 2 internal tables say ITAB1 and ITAB2

Loop at ITAB1

read table ITAB2 with key XXX = ITAB1-XXX.

if sy-subrc ne 0.

delete ITAB1.

endif.

Endloop.

Reward points if it helps you.

Regards,

Sudhakar.

12 REPLIES 12

Former Member
0 Kudos
763

Nagarajan,

Could you please post some data in the two internal tables that you are expecting?

Other wise, you will have to loop at one table, read the other table with the key in the work area and take action accordingly.

Regards,

Ravi

0 Kudos
763

Hi ravi,

Thanks for your immediate reply.

In my internal table i have matnr as key field in both tables.

regards

naga.

kanthimathikris
Employee
Employee
0 Kudos
763

Is this your question?

You wanted to delete record of one ITAB which is not in the other (or) vice versa

Former Member
0 Kudos
763

Hi,

Loop at one internal table1 .

Read theinternal table2 with some ctiteria to compare(Decide the deletion criteria based on some fields).

Then delete the rwo from internal table 2.

Former Member
764

Hi Nag,

If you have 2 internal tables say ITAB1 and ITAB2

Loop at ITAB1

read table ITAB2 with key XXX = ITAB1-XXX.

if sy-subrc ne 0.

delete ITAB1.

endif.

Endloop.

Reward points if it helps you.

Regards,

Sudhakar.

0 Kudos
763

Hi sudhakar,

Thanks..

I cannot do this without a loop?

regards

Naga.

former_member188685
Active Contributor
763
loop at itab1.
l_tabix = sy-tabix.
read table itab2 with some key..
if sy-subrc = 0.
if itab1 <> itab2.
delete itab1 ,itab2...
endif.
endif.

endloop.

Former Member
0 Kudos
763

You can use the table body operator to compare the contents of two internal tables, as shown here:

if it1[] = it2[].

To use this construct, the internal tables must have the same structure. If they do not, you will have to compare them manually, row by row.

This statement is true when it1 and it2 contain the same number of rows and the contents of each row are the same.

Using the delete statement, you can delete one or more rows from an internal table.

Syntax for the delete Statement

The following is the syntax for the delete statement.

delete it (a) [index n]

(b) [from i] [to j]

(c) [where exp]

where:

n, fn, and tn are numeric literals, variables, or constants.

exp is a logical expression involving components of it.

Former Member
0 Kudos
763

Hi Nag,

You can't do this without a Loop and Inside a read statement.

Regards,

Sudhakar.

Former Member
0 Kudos
763

Hi Nagarajan,

1. I don't think there is any direct (one-shot statement)

way to achieve this.

one has to do by writing some logic.

2. Loop at ITAB1.

Read table ITAB2 with key Field1 = ITAB1-Field1.

If sy-subrc <> 0.

delete ITAB1.

endif.

Endloop.

Do the same again with ITAB2.

Loop at ITAB2.

Read table ITAB1 with key Field1 = ITAB2-Field1.

If sy-subrc <> 0.

delete ITAB2.

endif.

Endloop.

3. If any better way is found, i will let u know.

Hope it helps.

Regards,

Amit M.

former_member186741
Active Contributor
0 Kudos
763

try:

loop at itab1.

read table itab2 with key matnr = itab1-matnr.

if sy-subrc <> 0.

delete itab1.

delete itab2 where matnr = itab1-matnr.

endif.

endloop.

kathylau007
Discoverer
0 Kudos
763

IF lt_tab1[] IS NOT INITIAL.

DATA(lr_vbeln) = VALUE psi_we_selopt_tt( FOR ls_tab1 IN lt_tab1
( sign = 'I'
option = 'EQ'
low = ls_tab1-vbeln ) ).

DELETE lt_tab2 WHERE vbeln in lr_vbeln.

endif.