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

Compare two internal tables

chetan_mishra
Participant
0 Likes
6,000

Hi,

I have two internal tables ITAB and ITAB1. I have to compare the two tables based on a Field 'MATKL'.

That is, ITAB contains a list of MATKL, and ITAB1 has MATKL and their corresponding MATNR's.

I have to keep only those records in ITAB1, which have a corresponding record in ITAB.

Please suggest.

15 REPLIES 15
Read only

surajarafath
Contributor
0 Likes
5,181

you can achieve this before loading the internal table ITAB1

Just in the Select Statement for ITAB1 use for all entries.

ex:

SELECT * FROM <TABLE>
INTO CORRESPONDING FIELDS OF TABLE ITAB1
FOR ALL ENTRIES IN ITAB
WHERE       MATKL = ITAB1-MATKL.

Read only

Former Member
0 Likes
5,181

Hi,

Your post i dint get ,can you elaborate clearly you want to fetch from single table and placed that same value in other table.

If you want to place common matnr in internal table use for all entries in the second query.

Regards,

G.Aditya

Edited by: Aditya.G on Dec 15, 2011 10:18 AM

Edited by: Aditya.G on Dec 15, 2011 10:21 AM

Read only

0 Likes
5,181

Hi Aditya,

My query is :

I have a table ITAB populated through various select logics, it contains the field 'MATNR''.

There's another table ITAB1, which fetches data (MATNR) from MARA for specific 'MATKL' (picked from another table).

I have to keep only those records in ITAB, for which there is a corresponding record in the table ITAB1, and delete the others.

Read only

0 Likes
5,181

Hi,

From which table you retrieve the data for the first query.

Read only

0 Likes
5,181

Then You try like this,

with this logic you delete the entries which doesnt have in ITAB

LOOP AT ITAB1 INTO WA_ITAB1.
READ TABLE ITAB WITH KEY MATKL = WA_ITAB1-MATKL TRANSPORTING NO FIELDS.
IF SY-SUBRC NE 0.
DELETE ITAB1 INDEX SY-TABIX.
ENDIF.
ENDLOOP.

Read only

0 Likes
5,181

Hi Ravi,

The first Internal Table is fetched from multiple tables, and some fields are calculated and then appended.

Read only

0 Likes
5,181

Hi Bachack,

// use this query

select * from mara into corresponding fields of table itab1 for all entries in itab where matnr = itab-matnr.

// This query retrieves the data for the revelant matnr in itab .

Read only

Former Member
0 Likes
5,181

Hi,

SELECT * FROM ITAB

INTO CORRESPONDING FIELDS OF ITAB1

WHERE MATKL = ITAB1-MATKL.

Warm Regards,

PavanKumar.G

Edited by: pavankumar.g on Dec 15, 2011 5:45 AM

Read only

0 Likes
5,181

Hi Pavan,

I tried this but I get an error " 'ITAB' is not defined in the ABAP dictionary as a table, projection view, or database view."

Read only

0 Likes
5,181

Hi Bach,

Try this one

SELECT * FROM MARA

INTO CORRESPONDING FIELDS OF TABLE ITAB1

FOR ALL ENTRIES IN ITAB

WHERE MATKL = ITAB1-MATkL.

Warm Regards,

PavanKumar.G

Edited by: pavankumar.g on Dec 15, 2011 6:01 AM

Edited by: pavankumar.g on Dec 15, 2011 6:11 AM

Read only

0 Likes
5,181

hi,

select statement won,t work for internal tables,it's only to fetch the data from data base table.that is the reason it is showing error.use DB table instead of internal table.

Thanks

Naresh

Read only

naresh_bammidi
Contributor
0 Likes
5,181

HI,

Use the following code ..

first create an internal table with required fields.

for eg: 

data:begin of it_final occurs 0,
          matnr type matnr,
          maktl  type maktl,
        end of it_final.

Loop at itab.
read table itab1 into wa with key where matnr = itab-matnr.
move wa-matnr to it_final-matnr.
move wa-maktl  to it_final-maktl.

append it_final.
clear it_final.

endloop.

Now you have required fields into your internal table

Edited by: naresh bammidi on Dec 15, 2011 5:58 AM

Read only

Former Member
0 Likes
5,181

Hi,

select * from mara into corresponding fields of table itab .

if itab is not initial.

select * from mara into corresponding fields of table itab1 for all entries in itab where maktl = itab-maktl.

endif.

Read only

Former Member
0 Likes
5,181

Hi,

Try below logic.

Data : LW_TABIX TYPE SYTABIX.

LOOP AT ITAB1 INTO WA_ITAB1.
CLEAR LW_TABIX.
LW_TABIX = SY-TABIX.
READ TABLE ITAB WITH KEY MATKL = WA_ITAB1-MATKL TRANSPORTING NO FIELDS.
IF SY-SUBRC NE 0.
DELETE ITAB1 INDEX LW_TABIX.
ENDIF.
ENDLOOP.

BR,

Vijay

Read only

Former Member
0 Likes
5,181

The Simplest solution for your query is :


LOOP AT ITAB1 INTO WA.
READ TABLE ITAB TRANSPORTING NO FIELD WITH KEY MATNR = WA1-MATNR AND MATKL = WA1-MATKL.
IF SY-SUBRC <> 0.
DELETE ITAB1 FROM WA.
ENDIF.
ENDLOOP.

Try it at your end.

Thanks,

Anmol.