09-05-2008 12:07 PM
Hi Experts,
I am using three tables in my program. From two table single field from each I need to pass into third table but before inserting the field values in third table i need to check if its present no need to insert else I need to insert. Even the two tables are not related at all. I used bellow code but I think its not efficient.. So plz any body help me and taking performance into an account.
select lifnr from tab1 into table it_tab1.
loop at it_tab1.
select zlifnr from tab3 into table it_tab3 where zlifnr = it_tab1-lifnr.
if sy-subrc = 0.
continue.
else.
tab3-zlifnr = tab1-lifnr.
insert tab3.
endif.
endloop.
select distinct sri from tab2 into table it_tab2.
loop at it_tab1.
select zsri from tab3 into table it_tab3 where zsri = it_tab2-sri.
if sy-subrc = 0.
continue.
else.
tab3-zsri = tab2-sri.
insert tab3.
endif.
endloop.
Is there any chance to combine both statements or quiry into one . plz help me, Thanks
Basu
09-05-2008 12:48 PM
Hi.....
Try like this...
select F1 from T1 into table ITAB1.
select F1 from T2 into table ITAB2.
select F1 F2 from T3 into table ITAB3.
loop at ITAB3.
loop at ITAB1.
if ITAB3-F1 is initial.
ITAB3-F1 = ITAB1-F1.
endif.
endloop.
modify itab.
endloop.
loop at ITAB3.
loop at ITAB2.
if ITAB3-F2 is initial.
ITAB3-F2 = ITAB2-F1.
endif.
endloop.
modify itab.
endloop.
This is better...
But here also i used loop inside the loop, Which is never prefers...
I think there will be link between T1 and T3 & T2 and T3.
Then we can avoid that loop inside the loop with code like following...
select F1 from T1 into table ITAB1.
select F1 from T2 into table ITAB2.
select F1 F2 from T3 into table ITAB3.
loop at ITAB3.
if ITAB3-F1 is initial.
read table ITAB1 with key F1 = comarision key from T3 table.
ITAB3-F1 = ITAB1-F1.
endif.
if ITAB3-F2 is initial.
read table ITAB2 with key F1 = comarision key from T3 table
ITAB3-F2 = ITAB2-F1.
endif.
modify itab.
endloop.
Thanks,
Naveen.I
09-05-2008 12:48 PM
Hi.....
Try like this...
select F1 from T1 into table ITAB1.
select F1 from T2 into table ITAB2.
select F1 F2 from T3 into table ITAB3.
loop at ITAB3.
loop at ITAB1.
if ITAB3-F1 is initial.
ITAB3-F1 = ITAB1-F1.
endif.
endloop.
modify itab.
endloop.
loop at ITAB3.
loop at ITAB2.
if ITAB3-F2 is initial.
ITAB3-F2 = ITAB2-F1.
endif.
endloop.
modify itab.
endloop.
This is better...
But here also i used loop inside the loop, Which is never prefers...
I think there will be link between T1 and T3 & T2 and T3.
Then we can avoid that loop inside the loop with code like following...
select F1 from T1 into table ITAB1.
select F1 from T2 into table ITAB2.
select F1 F2 from T3 into table ITAB3.
loop at ITAB3.
if ITAB3-F1 is initial.
read table ITAB1 with key F1 = comarision key from T3 table.
ITAB3-F1 = ITAB1-F1.
endif.
if ITAB3-F2 is initial.
read table ITAB2 with key F1 = comarision key from T3 table
ITAB3-F2 = ITAB2-F1.
endif.
modify itab.
endloop.
Thanks,
Naveen.I
09-05-2008 12:50 PM
Hi,
You need not check whether the entry is in the table or not,If you use the modify statement.
If you use Modify statement if the record is already there,it will be modified as specified,Otherwise a new record will be inserted.
So Your code can be optimized as follows which will be executed in much faster.
select lifnr from tab1 into table it_tab1.
select distinct sri from tab2 into table it_tab2.
loop at it_tab1.
tab3-zlifnr = tab1-lifnr.
modify tab3.
tab3-zsri = tab2-sri.
modify tab3.
endloop.
Regards,
Rama.
09-10-2008 9:06 AM
09-10-2008 9:07 AM