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: 

Difference between Select Single and Selct upto 1 row

Former Member
0 Kudos

Hi All,

Can anyone please expain what's the main difference between 'Select Single' and 'Select upto 1 row' with some example

regards

henry

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

When Select Single statement is used, all the keys should be specified. And more importantly SAP buffer is always bypassed with the use of this statement.

Select upto 1 rows can very well use the buffer in case the table has SAP Buffer declared in ABAP Dictionary.

Regards,

Vijay

13 REPLIES 13

Former Member
0 Kudos

Hi Pavan / Henry,

There was an on this sometime back. You might want to refer to that.

Regards,

Anand Mandalika.

0 Kudos

It's good,Anand

Former Member
0 Kudos

Hi,

To add to my post above,

When you say SELECT SINGLE, it means that you are expecting only one row to be present in the database for the condition you're going to specify in the WHERE clause. so that means, you will have to specify the primary key in your WHERE clause. Otherwise you get a warning.

SELECT UP TO 1 ROWS is used in cases where you just want to make sure that there is at least one entry in the database table which satisfies your WHERE clause. Generally, it is meant to be used for existence-check. You may not want to really use the values returned by the SELECT statement in this case (thought this may not necessarily be so).

And in each case the database optimizer may choose a different strategy to retrieve the data.

Regards,

Anand Mandalika.

0 Kudos

Hi Anand!!

Thakyoy for the reply.

Can please tell which one is used in whihc scenerios.Though I've used it but I've used it mechanically but not with much interpretation.please gimme some example.

database optimizer chooses different ways to choose data..can plz tell me which is 'performance improving one'.

regards

PNP

Former Member
0 Kudos

hm. I would say the following:

choose the select single if you can fully qualify the primary key of the table you want to read data from. you can be sure to receive exactly one record because there can be only one existing in the database which fits the selection criteria.

select up to one row? I generally use this one in order to check if entries exist. e.g. in select * from ekpo up to one rows where ebeln = 'whatever'. if you used the select single here then the extended syntax check would return an error. primary key not fully specified.

0 Kudos

Hi robert,thanks for the information..it's very helpful

regards

PNP

Former Member
0 Kudos

Hi,

When Select Single statement is used, all the keys should be specified. And more importantly SAP buffer is always bypassed with the use of this statement.

Select upto 1 rows can very well use the buffer in case the table has SAP Buffer declared in ABAP Dictionary.

Regards,

Vijay

0 Kudos

Mr Vijayakrishnan,

IF the buffer option is selected in ABAP Dictionary,still teh select single statement will by pass the buffer.

0 Kudos

Hi PNP,

If that is a question, then the answer is "yes".

Regards,

Anand Mandalika.

Former Member
0 Kudos

Hi Henry,

Both will pick up only one row.

but there is some difference.

when you r using the select up to one row. based on your key it will search the data base if it is there it will give. other wise it wont give you any thing.

but where as in Select single. it will search the data base based on your key . if it found any matching record it will display if there is no matching record in the data base it goes to the work area if there is any record it that work area it will pick up that record even though it is not satisfied with the gien key.

so when you r using you must clear the work area.

Thanks & Regards.

Raghu

0 Kudos

Hi,

Got this info from SDN ABAP FAQ's, u might have arrived solution already just to add with that.

<b>Knowing when to use SELECT SINGLE or SELECT ... UP TO 1 ROWS</b>

A lot of people use the SELECT SINGLE statement to check for the existence of a value in a database. Other people prefer to use the 'UP TO 1 ROWS' variant of the SELECT statement.

So what's the difference between using 'SELECT SINGLE' statement as against a 'SELECT .... UP TO 1 ROWS' statement ?

If you're considering the statements

SELECT SINGLE field INTO w_field FROM table.

and

SELECT field INTO w_field FROM table UP TO 1 ROWS. ENDSELECT.

then looking at the result, not much apart from the extra ENDSELECT statement. Look at the run time and memory usage and they may be worlds apart.

Why is this ?? The answer is simple.

The 'SELECT SINGLE' statement selects the first row in the database that it finds that fulfils the 'WHERE' clause If this results in multiple records then only the first one will be returned and therefore may not be unique.

The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.

Get the difference ??

If not, here is a good example, credit for this example goes to Richard Harper, a friend of mine on sapfans.com :

Create a Ztable called ZDifference with 2 fields in it, MANDT of type MANDT and POSNR of type POSNR. Make sure both of these are keys. Also create a table maintenance dialog for it (SE11->Utilities->Table Maintenance Generator). Fill the table with ten rows 000001-000010.

Then run the program shown below:

Code:

  • Program: Z_Difference

  • Purpose: A program that demonstrates the difference

  • between SELECT SINGLE and SELECT UP TO n ROWS.

  • This program requires the data table Z_DIFFERENCE

  • to have been created according to the structure

  • outlined in the text above and populated with

  • at least 10 records.

  • Creation Date: 21/04/2004

  • Requested By:

  • Reference Doc:

  • Author: R Harper

  • Modification History:

  • Date Reason Transport Who

Report Z_Difference

Message-id 38

Line-Size 80

Line-Count 0

No Standard Page Heading.

Start-Of-Selection.

Data: w_Single type Posnr,

t_Rows type standard table of Posnr

initial size 0

with header line.

Select single Posnr

from zDifference

into w_Single.

Select Posnr

into table t_Rows

from zDifference

up to 1 rows

order by Posnr descending.

Write 😕 'Select single:', w_Single.

Skip 1.

Write 😕 'Up to 1 rows :'.

Loop at t_Rows.

Write t_Rows.

EndLoop.

You should see the output:

Select single: 000001

Up to 1 rows : 000010

The first 'SELECT' statement selected the first record in the database according to any selection criterion in the 'WHERE' clause. This is what a 'SELECT SINGLE' does. The second 'SELECT' has asked the database to reverse the order of the records before returning the first row of the result.

In order to be able to do this the database has read the entire table, sort it and then return the first record. If there was no ORDER BY clause then the results would have been identical (ie both '000001') but the second select if given a big enough table to look at would be far slower.

Note that this causes a problem in the Extended Program Check if the full key is not specified in a 'SELECT SINGLE'. Replacing the 'SELECT SINGLE' by an "UP TO 1 ROWS" will give the same exact results without any warning but the program will run slower and consume more memory. This is a good example of a warning that we should ignore... considering you are sure of what you are doing !!

Thanks & Regards,

Judith.

0 Kudos

Just wanted to emphasize to some details already mentioned in Judith post, that is in SELECT UP TO n ROWS you can specify grouping and ordering and so this kind of query could be used to find out "TOP 10" of something. On the contrary to some above posts I would primarely use SELECT SINGLE to check just an existance in spite of the warning just because SELECT SINGLE statement is shorter than SELECT UP TO 1 ROWS ... ENDSELECT.

0 Kudos

Forgive my ignorance, but I have read several postings concerning the question in this and other forums and yet I am not convinced that there is a notable difference in the statements on database level (yes there is one concerning the sap table buffer where you decide if you want to use single buffered or generic key resp. fully bufferd mode but apart from that?). At least not in our system with an oracle database.

The above mentioned statement is not fair since with select * up to 1 rows an order statement is used. It requires a "sort" on database level. Since this is not possible with a select single statement the difference in performance is explainable in this case. The same applies if you use select max ...or other aggregations but then you would not expect the same performance behavour, would you?

if you do not add the order option to the select statement select * up to 1 rows and select single * dissolves in exactly the same native sql statement - at least in our system - so how and why should there be a performance difference?

Christian