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

Better code ....

Former Member
0 Likes
697

Hi,

Of the below 2 set of codes (Set 1 and set 2 ) which is more optimized? And do i get the same output in both the set of codes?

Set 1

SELECT single JOBNAME

FROM tbtco

into w_jobname

WHERE jobname = jobname AND

( status = 'R' OR status = 'Y' ).

IF sy-subrc = 0.

w_jobnam_found = 'Y'.

ENDIF.

set 2

SELECT * FROM tbtco WHERE jobname = jobname

AND ( status = 'R' OR status = 'Y' ).

IF sy-subrc = 0.

jobname_found = 'Y'.

EXIT.

ENDIF.

ENDSELECT.

regds,

M..

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
667

Hi,

Set 1 is more optimised and better than Set2. SAP itself recommends not to use Select..Endselect because of the performance issue.

As you juset need to check whether any entry exist for the given criteria then first Set is perfect.

Thanks & Regards,

Navneeth K.

7 REPLIES 7
Read only

Former Member
0 Likes
667

Hi,

If you are selecting only one job, then select single is enough and select ...endselect is not.

However you need to make sure that when you use select single, you use the primary key, always, in the where clause.

Since you aren't using the primary key to select from the table tbtco, there are chances that you will get multiple records and select single will not work as expected.

regards,

Advait

Edited by: Advait Gode on Oct 24, 2008 10:58 AM

Read only

former_member195383
Active Contributor
0 Likes
667

1st one is better...

2nd one is similar to aloop and is not usually preffered

Read only

Former Member
0 Likes
667

Hi madhu,

First of all you will not get same result if there are more data to satisfy your where condition. because SELECT... ENDSELECT works like a loop and will all the data which will satis fy the codition and SELECT SINGLE will choose only the first data which satisfy the condition.

Secondly,if you consider the performance issue , obviously 1st one is better as in SELECT...FROM...INTO works in batch process an losad all the data in the internal table and send it to apps server unlike SELECT... ENDSELECT , where each datum is fetch separately . SELECT....ENDSELECT cuase more load to data-server.

Regards,

Anirban

Read only

ThomasZloch
Active Contributor
0 Likes
667

why don't you just run it yourself and find out?

Read only

Former Member
0 Likes
667

Hi Madhu,

The Set of code that contains "select single" is the optmized part as it contact database only once and retreives record once based on the given condition.

Where as the second set that has SELECT and ENDSELECT, has to contact database for every single record until the given condition matches....this reduces the performance to a very large extent.

Best Regards,

Ram.

Read only

JozsefSzikszai
Active Contributor
0 Likes
667

If the objective is to only check if any entry exists in the table with a specific jobname and the statuse has to be R or Y, than the best way is:

SELECT COUNT( * )
FROM tbtco
WHERE jobname EQ jobname
AND (status EQ 'R' OR status EQ 'Y').
IF sy-subrc EQ 0.
==> entry exists
ELSE.
==> entry does not exist
ENDIF.

Read only

Former Member
0 Likes
668

Hi,

Set 1 is more optimised and better than Set2. SAP itself recommends not to use Select..Endselect because of the performance issue.

As you juset need to check whether any entry exist for the given criteria then first Set is perfect.

Thanks & Regards,

Navneeth K.