Application Development 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: 

doubt in select statement

Former Member
0 Kudos

1. can we write select statement with in loop and endloop.? and if i write what will be happen?

Please help me.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Yes,You can write select between loop and Endloop.

But when you use between loop and Endloop, it goes to the database everytime When select statement is executed everytime. So it delays the execution process.

So avoid using select between loop and Endloop.

10 REPLIES 10

amit_khare
Active Contributor
0 Kudos

Welcome to SDN.

You can write but it will create performance issue if the data is too large.

Instead select all the records in an internal table and use READ statement inside the loop-endloop.

Regards,

Amit

Reward all helpful replies.

0 Kudos

HI,

You can write but its very BAD in performance.

INSTEAD USE

SELECT INTO TABLE itab.

And then LOOP AT itab ENDLOOP.

Regards,

Sesh

.

mohammed_moqeeth
Active Participant
0 Kudos

Hi Mahesh,

Yes you can write a select statement inside a loop, but it effects performance.

Instead of this you can collect the data from database into one internal table and then within loop ..endloop you can read that table.

<b>example.</b>

loop at i_ekko into wa_ekko.

read table i_ekpo into wa_ekpo with key ebeln = wa_ekko-ebeln.

if sy-subrc = 0.

write \ 'found'.

else.

write 'not found'.

endif.

endloop.

<b>Reward points if you feel helpful.

Cheers !

Moqeeth.</b>

Former Member
0 Kudos

Hi mahesh,

we can use select stmt in loop but performance wise it is not better instead of that we can use for all entries.

regards

swetha.

0 Kudos

hi ,

thanx for ur reply,

but if i write select statement with in loop and endlop, i am not getting output, instead of showing list , it is showing message that ' you may not delete itab or u may not overright itab' on status bar.

can u help me with simple example ( with normal select statement but not for all entries).

thanx.

selvakumar_mohan
Active Participant
0 Kudos

SELECT * INTO TABLE internal_tab.

LOOP AT internal_tab

<use any field inside the internal table.>

ENDLOOP.

As others said using select* within loop leads to performance degradation if data is very large in the table..

Former Member
0 Kudos

hi

u can write select statement within a loop.during each iteration it will fetch data from DB table, system load increases which leads to poor performance of the system.

try as follows:

select * from DBtable into table itab where ..

loop at itab.

write:

endloop.

Former Member
0 Kudos

Yes,You can write select between loop and Endloop.

But when you use between loop and Endloop, it goes to the database everytime When select statement is executed everytime. So it delays the execution process.

So avoid using select between loop and Endloop.

Former Member
0 Kudos

Hi mahesh,

its not a good pratice to write a select statement inside loop..endloop....

else it will result in a performance issue....

its better to write the selct statement outside loop and in loop..enloop u can use it by READ command.....and then check for sy-subrc value

hope this will help u out..

please reward incase usefull...

regards,

prashant

Former Member
0 Kudos

<b>SELECT command - Information and example code of how to use the select command</b>

The select command is the most fundamental function of writing ABAP programs allowing the retrieval ofdata from SAP database tables. Below are a few examples of the various ways of selecting data.


*Code to demonstrate select command
*Code to demonstrate select into internal table command
TYPES: BEGIN OF t_bkpf,
*  include structure bkpf.
  bukrs LIKE bkpf-bukrs,
  belnr LIKE bkpf-belnr,
  gjahr LIKE bkpf-gjahr,
  bldat LIKE bkpf-bldat,
  monat LIKE bkpf-monat,
  budat LIKE bkpf-budat,
  xblnr LIKE bkpf-xblnr,
  awtyp LIKE bkpf-awtyp,
  awkey LIKE bkpf-awkey,
 END OF t_bkpf.
DATA: it_bkpf TYPE STANDARD TABLE OF t_bkpf INITIAL SIZE 0,
      wa_bkpf TYPE t_bkpf.

TYPES: BEGIN OF t_bseg,
*include structure bseg.
  bukrs     LIKE bseg-bukrs,
  belnr     LIKE bseg-belnr,
  gjahr     LIKE bseg-gjahr,
  buzei     LIKE bseg-buzei,
  mwskz     LIKE bseg-mwskz,         "Tax code
  umsks     LIKE bseg-umsks,         "Special G/L transaction type
  prctr     LIKE bseg-prctr,         "Profit Centre
  hkont     LIKE bseg-hkont,         "G/L account
  xauto     LIKE bseg-xauto,
  koart     LIKE bseg-koart,
  dmbtr     LIKE bseg-dmbtr,
  mwart     LIKE bseg-mwart,
  hwbas     LIKE bseg-hwbas,
  aufnr     LIKE bseg-aufnr,
  projk     LIKE bseg-projk,
  shkzg     LIKE bseg-shkzg,
  kokrs     LIKE bseg-kokrs,
 END OF t_bseg.
DATA: it_bseg TYPE STANDARD TABLE OF t_bseg INITIAL SIZE 0,
      wa_bseg TYPE t_bseg.


*Select directly into an internal table
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
       dmbtr mwart hwbas aufnr projk shkzg kokrs
  FROM bseg
  INTO TABLE it_bseg.


* Select directly into an internal table where fields are in a
* different order or not all fields are specified 
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
       dmbtr mwart hwbas aufnr projk shkzg kokrs
  FROM bseg
  INTO CORRESPONDING FIELDS OF TABLE it_bseg.


*Select... endselect command
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
       dmbtr mwart hwbas aufnr projk shkzg kokrs
  FROM bseg
  INTO wa_bseg.

  APPEND wa_bseg TO it_bseg.
ENDSELECT.


*Select FOR ALL ENTRIES command
SELECT bukrs belnr gjahr bldat monat budat xblnr awtyp awkey
  UP TO 100 ROWS
  FROM bkpf
  INTO TABLE it_bkpf.

IF sy-subrc EQ 0.
* The FOR ALL ENTRIES comand only retrieves data which matches
* entries within a particular internal table.
  SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
         dmbtr mwart hwbas aufnr projk shkzg kokrs
    FROM bseg
    INTO TABLE it_bseg
    FOR ALL ENTRIES IN it_bkpf
    WHERE bukrs EQ it_bkpf-bukrs AND
          belnr EQ it_bkpf-belnr AND
          gjahr EQ it_bkpf-gjahr.
ENDIF.

reward points if it is usefull ....

Girish