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

Deleting records from compared tables...

Former Member
0 Likes
1,734

Hi all,

I know there are a bunch of posts regarding this issue...but I don't see one that quite fits this scenario. I have two tables with the exact same structure...with key field matnr. I want to delete all of those records from table A where the material does NOT exist in table B. Is there a terse way to accomplish this?

1 ACCEPTED SOLUTION
Read only

suresh_datti
Active Contributor
0 Likes
1,706

sort: itaba,itabb.

loop at itaba.

read table itab b with key matnr = itaba-matnr.

if sy-subrc ne 0.

delete itaba.

endif.

endloop.

~Suresh

sort: itaba,itabb.

loop at itaba.

read table itab b with key matnr = itaba-matnr.

if sy-subrc ne 0.

delete itaba.

endif.

endloop.

~Suresh

13 REPLIES 13
Read only

suresh_datti
Active Contributor
0 Likes
1,707

sort: itaba,itabb.

loop at itaba.

read table itab b with key matnr = itaba-matnr.

if sy-subrc ne 0.

delete itaba.

endif.

endloop.

~Suresh

Read only

0 Likes
1,706

Suresh and Ravi,

you are real good contributors to this forum and i like that.

Not that you give a LOT of answers, your answers even porve quality.

BUT i have one thing. People asking questions here are sometimes not TOO used to the ABAP syntax and other concepts.

SAP noticed that themselves and made some concepts "obsolete", to get a code which is better to read. Especially for newbies.

And well HEADER LINES are obsolete by now.

People not beeing really used to ABAP will have really big Problems in reading things like:

Loop at itab.

...

endloop.

or even harder:

modify itab.

this shall not be critic in any way, but rather a suggestion on how you can help the better. Accoring to your number of posts thats what you actually want.

thx for convenience.

Read only

0 Likes
1,706

I was just about to say that I'm making every effort to develop my code without the use of header lines. How might I implement a similar solution without using a header line?

Read only

0 Likes
1,706

DATA: itab1 type ???,

itab2 type ???,

wa1 like line of itab1.

loop at itab1 into wa1.

read table itab2 with key matnr = wa1-matnr.

if sy-subrc <> 0.

delete from itab1 where matnr = wa1-matnr.

endif.

endloop.

this is a piece of code withput header lines.

Read only

0 Likes
1,706

The second line of the code snippet is still looking for a header line....it throws an error at itab2.

Read only

0 Likes
1,706

oh sorry i was too much in a hurry.

DATA: itab1 type ???,

itab2 type ???,

wa1 like line of itab1,

wa2 like line of itab1.

loop at itab1 into wa1.

read table itab2 into wa2 with key matnr = wa1-matnr.

if sy-subrc <> 0.

delete from itab1 where matnr = wa1-matnr.

endif.

endloop.

this is a piece of code withput header lines.

Read only

0 Likes
1,706

I got the work area part figured out. But...I get a strange error on the delete that says the table is not active or does not exist. Can I delete form a table while I'm looping over it?

Read only

0 Likes
1,706

Use delete itab1 not delete from

~Suresh

Read only

0 Likes
1,706

Just use DELETE FROM itab1.

Read only

0 Likes
1,706

Replace

delete from itab1 where matnr = wa1-matnr.

with

delete itab1 where matnr = wa1-matnr.

delete from option is used when you delete entires in the database table not an internal table. That is the reason, you get the synatx error that the table is not active.. since itab1 is not in the dictionary.

~Suresh

Read only

Former Member
0 Likes
1,706

Hi Mathhew,

these is no terse way to do this. A liitle code nugget will do it for you.

loop at it_a.
lv_tabix = sy-tabix.
read table it_b with key matnr = it_a-matnr.
if sy-subrc <> 0.
delete it_a index lv_tabix.
endif.
endloop.

Regards,

Ravi

Read only

Former Member
0 Likes
1,706

loop at itab1 into wa1.

read table itab2 with key matnr = wa1-matnr.

if sy-subrc NE 0.

delete from itab1 where matnr = wa1_matnr.

endif.

endloop.

Message was edited by:

Florian Kemmer

Read only

Former Member
0 Likes
1,706

Hi,

If those are internal tables then you can use any of these experts code, if those are database tables then you need to include one more statement ...

DELETE dbtab FROM TABLE itab.

loop at itab1.

read table itab2 with key matnr = itab1-matnr.

if sy-subrc NE 0.

  • DELETE dbtab FROM wa

DELETE dbtab FROM itab1.

endif.

endloop.