‎2007 May 25 4:43 PM
hello friends,
following is my scenario.
it seems little tricky and complex. can some one help me with this.
i never used select and endselect.
can we transfer data beteween different itabs and also get data from ztable at a time inside select and endselect
loop at itab1
select ztable3 where field4 = itab1-field4.
move itab1-field1 to itab2-field1.
move ztable3-field3 to itab2-field3.
itab2-field5 = ztable3-field7 * itab1-field6 * itab1-field5
endselect.
endloop
‎2007 May 25 4:50 PM
Hi Saritha,
The select .. endselect works as a loop fetching single record for every loop pause..
as you kept a select inside a loop .. you have 2 loops.. going to the database unnecessarily so many times is not a good idea...
instead you can write
select * from ztable3 for all entries in itab1 where field4 = itab1-field4.
then ...
loop at itab1.
read table ztable3 with key field4 = itab1-field4.
..move your fields...
append itab2.
endloop.
Regards,
Vidya.
‎2007 May 25 4:46 PM
Yes it is possible.
SELECT...ENDSELECT is like a LOOP only.
SELECT * INTO ITAB1
FROM TAB.
LOOP AT ITAB1.
ITAB2-FIELD = ITAB1-FIELD1 * X.
APPEND ITAB2.
ENDLOOP.
is same as
SELECT * FROM TAB.
ITAB2-FIELD = TAB-FIELD1 * X.
ENDSELECT.
‎2007 May 25 4:50 PM
Hi Saritha,
The select .. endselect works as a loop fetching single record for every loop pause..
as you kept a select inside a loop .. you have 2 loops.. going to the database unnecessarily so many times is not a good idea...
instead you can write
select * from ztable3 for all entries in itab1 where field4 = itab1-field4.
then ...
loop at itab1.
read table ztable3 with key field4 = itab1-field4.
..move your fields...
append itab2.
endloop.
Regards,
Vidya.
‎2007 May 25 4:51 PM
Hello Saritha,
Do not use select endselect and it gives more performance issue
loop at itab1
select * from ztable3 into table itab2
where field4 = itab1-field4.
loop at itab2.
move itab1-field1 to itab2-field1.
move ztable3-field3 to itab2-field3.
itab2-field5 = ztable3-field7 * itab1-field6 * itab1-field5
endloop.
endloop
Reward Points if it is helpful
Thanks
Seshu
‎2007 May 25 4:53 PM
Hi saritha,
we can write any no.of selects or select.. endselect( also another form of loop) statements within the loop. but it leads to poor performance since you are trying to access the same database table many times in the loop.
use for all entries clause to improve the performance.
Thanks
Srinivas
‎2007 May 25 4:53 PM
hi darshil
thanks for response,
here i just want to know how to handle a situation where in data transfers and calculations happen between two itabs and a ztable
‎2007 May 25 4:59 PM
What exactly is the scenario? You want to move data to itab2 or you want to get data from itab1, itab2 and ztable3?
If you move data to itab2 or you want to get data from itab1, itab2 and ztable3:
loop at itab1
select ztable3 where field4 = itab1-field4.
*Use a read statement:
<b>READ TABLE ITAB2 WITH KEY ...
(or you can use LOOP AT ITAB2 in case of multiple records)</b>
move itab1-field1 to itab2-field1.
move ztable3-field3 to itab2-field3.
itab2-field5 = ztable3-field7 * itab1-field6 * itab1-field5
endselect.
endloop
If ITAB2 is your destination table then:
loop at itab1
select ztable3 where field4 = itab1-field4.
move itab1-field1 to itab2-field1.
move ztable3-field3 to itab2-field3.
itab2-field5 = ztable3-field7 * itab1-field6 * itab1-field5
<b>APPEND ITAB2.</b>
endselect.
endloop
‎2007 May 25 4:55 PM
‎2007 May 25 4:58 PM
Saritha,
we cannot use a read on a database table directly.
we need to make a select from the ztable into an internal table and then read on that internal table.
Regards,
Vidya.