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

SELECT SQL in a LOOP statement

Former Member
0 Likes
2,489

Hi all,

As known, it's not recommended to write SELECT when doing loop for an internal table.

But this time my problem is that I have to use an SAP standard FM to get data from DB in a LOOP.

The internal table is large and it causes some memory shortage and time issues when I run it.

And it's not easy to do data fetching by myself so that I am trying to figure out another way.

Is there something like always make DB connection available, avoiding 'open'(like PREPARE) and 'close' a lot of times, like:


OPEN DB connection.

LOOP AT it.

SELECT xxx FROM xxx

ENDLOOP.

CLOSE DB connection.


Is it possible to do that?

9 REPLIES 9
Read only

Former Member
0 Likes
1,915

Which FM are you using for fetching data from database?

Read only

vinodkumar_thangavel
Participant
0 Likes
1,915

This message was moderated.

Read only

Former Member
0 Likes
1,915

hi ming,

you can use database cursor concept where you can mention size.

Read only

0 Likes
1,915

can you briefly explain what it is, thanks.

Read only

Former Member
0 Likes
1,915

Hi,

    You can write SELECT query outside LOOP  using FOR ALL ENTRIES w.r.t internal table "it" and then read this structure in within the LOOP.

SELECT XXX

INTO TABLE it_one

FOR ALL ENTRIES XXX
WHERE XXX

LOOP AT it.

READ TABLE it_one INTO wa_one WITH KEY XXX = it-XXX.

ENDLOOP.

Regards,

GJ

Read only

DominikKraemer
Active Participant
0 Likes
1,915

Hello,

you could work with DB cursor to process only 100 entries at one time:

OPEN CURSOR dbcur FOR

  SELECT *

  FROM spfli

  ORDER BY carrid.

DO.

  FETCH NEXT CURSOR dbcur INTO TABLE spfli_tab PACKAGE SIZE 100.

     IF sy-subrc <> 0.

       EXIT.

     ENDIF

* Your logic here for 100 entries each

ENDDO.

CLOSE CURSOR: dbcur.

Read only

Former Member
0 Likes
1,915

Hi,


You can eliminate the data using 'FOR ALL ENTRIES ' statement OR you can use that statement for performance tunning:



%_HINTS ORACLE

               '..........'.

refer the following links,

http://www.sapfans.com/forums/viewtopic.php?p=883185&sid=37b22c7f1a6421ccb8966c9da421a540

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,915

Which FM do you call in the LOOP?

Regards,

Raymond

Read only

former_member228514
Participant
0 Likes
1,915

Try to use package option. Take some interval of records  while fetching data