2008 May 22 7:18 PM
Hi All,
Whats the basic difference between the below two
SELECT SINGLE
and
SELECT UPTO 1 row ,
as both will fetch only 1 record.
and is it true that select single will fetch data directly from database bypassing the buffer?
cheers
kiran.
2008 May 22 7:31 PM
Hi Kiran,
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 or lack of, applies any aggregate, ordering or grouping functions to them and then returns the first record of the resultant result set.
Inorder to check for the existence of a record then it is better to use SELECT SINGLE than using SELECT ... UP TO 1 ROWS since it uses low memory and has better performance
Best regards,
raam
Hi Kiran,
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 or lack of, applies any aggregate, ordering or grouping functions to them and then returns the first record of the resultant result set.
Inorder to check for the existence of a record then it is better to use SELECT SINGLE than using SELECT ... UP TO 1 ROWS since it uses low memory and has better performance
Best regards,
raam
2008 May 22 7:27 PM
hi,
Select single has to be used with a where condition that has all the key fields:
It will always return a unique record(If a match is found).
Select upto 1 rows would get your the first record if multiple matches are found.
and select up to has to be end with endselect statements.
*******************************************
According to SAP Performance course the SELECT UP TO 1 ROWS is faster than SELECT SINGLE because you are not using all the primary key fields.
select single is a construct designed to read database records with primary key. In the absence of the primary key, it might end up doing a sequential search, whereas the select up to 1 rows may assume that there is no primary key supplied and will try to find most suitable index.
The best way to find out is through sql trace or runtime analysis.
Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
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.
Mainly: to read data from
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.
Mainly: to check if entries exist.
************************************************************************
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.
Knowing when to use SELECT SINGLE or SELECT ... UP TO 1 ROWS
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.
Points if useful.
/KIRAN
2008 May 22 7:31 PM
Hi Kiran,
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 or lack of, applies any aggregate, ordering or grouping functions to them and then returns the first record of the resultant result set.
Inorder to check for the existence of a record then it is better to use SELECT SINGLE than using SELECT ... UP TO 1 ROWS since it uses low memory and has better performance
Best regards,
raam
2008 May 22 7:31 PM
2008 May 23 3:25 AM
Hi,
according to me............
SELECT SINGLE takes very less time. But full KEY values required to b provided. mainly used to read data. If more than 1 similar record found than it will return the 1st record.
SELECT UPTO....used to check whether entries exist or not. If KEY fields are unknown than its meaningful to use. one more thing is if more than 1 record but small ammount than we can use this one. aggregate operation r done.
Thanks.
2008 May 23 5:38 AM
hi,
consider the following example:
SELECT SINGLE field
INTO w_field
FROM table.
SELECT field
INTO w_field
FROM table
UP TO 1 ROWS.
ENDSELECT.
the select single statement selects the first row that
satisfies the where condition If this results in multiple records then only the first one will be returned.
The 'SELECT .... UP TO 1 ROWS' statement selects all of the relevant records that are defined by the WHERE clause
and then returns the first record of the resultant result set.
rward if useful,
thanks and regards
2008 May 23 5:52 AM
Hi Kiran,
tables : vbak.
select single vbeln kunnr from vbak into corresponding fields of vbak where kunnr = '0000001000'.
write : vbak-kunnr,
vbak-vbeln .
select vbeln kunnr from vbak into corresponding fields of vbak where kunnr = '0000001000'.
endselect.
write : vbak-kunnr,
vbak-vbeln .
in the first select single statement it will fetch the first record where kunnr = '0000001000'.
in the second select statment will give you the last record where kunnr = '0000001000'.
for this once open the table VBAK and give Kunnr = '0000001000'.
and check ur results.
Thanks & regards,
S.Gangi reddy.
2008 May 23 5:54 AM
hi,
Select single * from KNA1 where clause
---It fetches single record from the database, based on the condition you specified in the where clause.
Select singles are also used to look up values from a database where that value is going to be constant for the duration of the program run, or the value is being used to validate some user entry.
Where as select * from kna1 up to 1 row
---Fetches first record if the condition specified in the where clause is satisfied, otherwise it doesn't fetch any record.
Code:
SELECT SINGLE field
INTO w_field
FROM table.
and
Code:
SELECT field
INTO w_field
FROM table
UP TO 1 ROWS.
ENDSELECT.
regards,
Rajyalakshmi.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |