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: 

Use of select... end select

Former Member
0 Kudos

Hi

Why do we use select and endselect... is there any alternative for that

12 REPLIES 12

former_member223537
Active Contributor
0 Kudos

SAP doesnt recommend using SELECT ENDSELECT bcz of performance issues.

Instead use the following:

SELECT MATNR

ERSDA

from MARA

<b> into table i_mara</b>.

i.e. fetch the data into internal table instead of variables.

Former Member
0 Kudos

hi ram,

select endselect is used when there are multiple records are to be processed and then kept internal table.

Use internal table without header line and move correponding records in it.

Example:

TYPES: BEGIN OF st_bsik,

bukrs TYPE bsik-bukrs,

lifnr TYPE bsik-lifnr,

belnr TYPE bsik-belnr,

wrbtr TYPE bsik-wrbtr,

shkzg TYPE bsik-shkzg,

waers TYPE bsik-waers,

budat TYPE bsik-budat,

gjahr TYPE bsik-gjahr,

END OF st_bsik.

DATA: it_bsik TYPE STANDARD TABLE OF st_bsik INITIAL SIZE 0,

it_bsik_wa TYPE st_bsik.

SELECT bukrs lifnr wrbtr shkzg waers belnr budat gjahr FROM bsik

INTO CORRESPONDING FIELDS OF TABLE it_bsik.

That fill fill all records in it_bsik internal table.

To Write these records:

LOOP AT it_bsik INTO it_bsik_wa.

write 😕 it_bsik_wa-gjahr.

ENDLOOP.

Regards,

Sachin.

Former Member
0 Kudos

Select .. endselect is used if you want to operate on each record that you will get from the database.

If you are interested in only 1 record, the alternative is

Select SINGLE * ... This will not require Endselect.

Hope this will help you.

~ Ramanath.

sailaja_vadlamudi
Contributor
0 Kudos

Hi

If the result of the selection is meant to be a table, the data is usually read line by line within a processing loop introduced by SELECT and concluded by ENDSELECT . For each line read, the processing passes through the loop once.

If the result of the selection is meant to be a single record, the closing ENDSELECT is omitted.

For select single

SELECT SINGLE * FROM SFLIGHT
                WHERE
                  CARRID   = 'LH '      AND
                  CONNID   = '0400'     AND
                  FLDATE   = '19950228'.
SEATSFREE = SFLIGHT-SEATSMAX - SFLIGHT-SEATSOCC

.

Select with into

SELECT COUNT( DISTINCT CARRID )
       INTO COUNT FROM SPFLI
       WHERE
         CITYTO = 'NEW YORK'.

They can avoid endselect .

Sail

gopi_narendra
Active Contributor
0 Kudos

select endselect is just like a select with in a loop.

see the below examples

<u>using select endselect</u>

select * from vbak.

append vbak to it_vbak.

endselect.

<u>alternative of using select endselect</u>

select * from vbak into table it_vbak.

Hope it is clear

Regards

Gopi

former_member197281
Active Participant
0 Kudos

hi,

Select Endselect has one major drawback, as anyone who's debugged a program with such a construct will know... a commit work, such as the debugger does every now and then, will destroy the cursor of the select/endselect, thereby triggering a short dump.If one of the statements in a SELECT ... ENDSELECT loop results in a database commit, the cursor belonging to the SELECT ... ENDSELECT loop is lost and the processing terminates with a runtime error. Since each screen change automatically generates a database commit, statements such as CALL SCREEN , CALL DIALOG , CALL TRANSACTION or MESSAGE are not allowed within a SELECT ... ENDSELECT loop. If you process your data only once, use a SELECT-ENDSELECT loop instead of collecting data in an internal table with SELECT ... INTO TABLE. Internal table handling takes up much more space

Reward points if helpful,

Regards,

jinesh

Former Member
0 Kudos

Thanks for ur timely help

Ram....

varma_narayana
Active Contributor
0 Kudos

Hii Ram..

Usually we will never use SELECT .. ENDSELECT as it gives very poor performace.

But whenever we read a single Record we will use it as an alternative for SELECT SINGLE

Eg:

SELECT * FROM MARC INTO WA_MARC UP TO 1 ROWS WHERE MATNR = P_MATNR.

ENDSELECT.

And other scenario is when we perform AGGREGATE OPERATIONS.when the Result is only one row.

SELECT SUM( LABST ) FROM MARD INTO V_LABST UP TO 1 ROWS

WHERE MATNR = P_MATNR.

ENDSELECT.

Note: In The Above scenario we cannot use SELECT SINGLE..

<b>reward if Helpful</b>

Former Member

Hello,

Consider the following scenario

data : int_dath type table occurs 0 with header line. "with header line

table : key1 key2 key3 Var1 Var2 Var3

record1 : X Y Z 1 2 3

record2 : X Y M 4 5 6

in our program we need to pass just key1 and key2 and get all relevant records

..that means if we write :

<i>Select * from table into int_dat where key1 = 'X' and Key2 = 'Y'.</i>

just one record will come....but more than one records are there for the key combination....If you do a syntax check then it will ask for <i>"endselect"</i>

because we are trying to pass multiple records into the header line

<b>One Alternative Solution:</b>

<i>Select * from table into <b>table int_dath</b> where key1 = 'X' and Key2 = 'Y'.</i>

if we mention <b>table</b> keyword before the internal table..then the select statement will pass all the records with key1 = 'X' and Key2 = 'Y' to int_dath

There we can do a read operation or looping whichever is suitable fo r the requirement

SAP never recommends select endselect and it is total <b>performance disaster</b> if we use loops or nested selects inside a select...endselect

Please check the following SAP notes on performance:

<b>If a SELECT - ENDSELECT loop contains a statement that triggers a database commit, the cursor belonging to the loop is lost and a program termination and runtime error occur. Remote Function Calls and changes of screen always lead to a database commit. The following statements are consequently not allowed wihtin a SELECT-ENDSELECT loop: CALL FUNCTION ... STARTING NEW TASK , CALL FUNCTION ... DESTINATION , CALL FUNCTION ... IN BACKGROUND TASK , CALL SCREEN, CALL DIALOG, CALL TRANSACTION, and MESSAGE.</b>

<u>Reward if helpful and please respond if you have clarity with the explanation above</u>

Regards

Byju

Former Member
0 Kudos

hi

check the below link

http://wiki.sdn.sap.com/wiki/display/ABAP/SELECTStatementsandCURSORstatement-Performance+Analysis.

Former Member
0 Kudos

SELECT  ZHR_CODE ZHR_PROD_LINE
     INTO (lv_test-KEy, lv_test-text)
     FROM ZHR_C_BUS_PROD
     where GSBER eq lv_busarea.
     append lv_test to lt_test.
   ENDSELECT.

How do you write this without endselect?

0 Kudos

Read the online SELECT documentation or press F1 key on select statement. Also, notice that the answer to your question is already in this old thread.

(Look for internal table in the document, also  read the Rules of Engagement.)

Regards,

Raymond