Hi folks,
I have a requirment where i have to write select queries with in a loop, so its taking so much time to execute the program (i used this method since i can;t use READ to write complex queries).
Is there any other alternative to access data from an internal table using complex queries.
Thanks in Advance,
Bharath.
Hi Bharath,
Most of the queries which are written within a loop can be avoided by proper "Select" queries and using either LOOP..ENDLOOP or READ statement. Since you haven't mentioned your scenario, I hope the below scenario would be helpful to you.
for eg:
if scenario is
LOOP at i_itab.
select single db_field1 db_field2
from database_table
into (var_field1, var_field2)
where db_field3 = i_itab-field1
and db_field4 = '45'.
ENDLOOP.
The above code can be written better as :
select db_field1 db_field2
from database_table
into table i_itab2
where db_field4 = '45'.
LOOP at i_itab1.
READ TABLE i_itab2 with key db_field3 = i_itab1-field1
ENDLOOP.
In case, it is not an equal to (=) scenario, then you can use, the loop..exit..endloop.
LOOP at i_itab1.
LOOP AT i_itab2 where db_field3 > i_itab1-field1.
EXIT.
ENDLOOP.
ENDLOOP.
I hope you find this useful.
Regards,
Subramanian V.
If I understand correctly, you want to run a SELECT statement using several rows of an internal table as WHERE clause criteria?
If so, use the "FOR ALL ENTRIES IN " keywords in SELECT statement. Refer to SELECT statement in help.sap.com for details.
Regards,
D.
Add a comment