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

Select ....

Former Member
0 Likes
740

Whats the difference betn. Select Single.. and Select.. Upto 1 row...?

Thanks

1 ACCEPTED SOLUTION
Read only

varma_narayana
Active Contributor
0 Likes
702

Hi..

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

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

Eg: When the Full primary key is not specified in the WHERE clause.

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..

reward if Helpful.

6 REPLIES 6
Read only

Former Member
0 Likes
702

Hi,

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.

Regards,

Bhaskar

Read only

Former Member
0 Likes
702

Regards

Vasu

Read only

Former Member
0 Likes
702

Hi Sagar,

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.

Thanks,

Vinay

Read only

Former Member
0 Likes
702

in select single in where condition we use all primary keys

in up to 1 rows no need to use

........

Read only

varma_narayana
Active Contributor
0 Likes
703

Hi..

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

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

Eg: When the Full primary key is not specified in the WHERE clause.

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..

reward if Helpful.

Read only

Former Member
0 Likes
702

Hi,

Reading a Single Line

To read a single entry from the database, use the following:

SELECT SINGLE <cols> ... WHERE ...

To ensure that the line can be uniquely identified, you must specify values for all of the fields of the primary key of the table in the WHERE clause. If the WHERE clause does not contain all of the key fields, the syntax check produces a warning, and the SELECT statement reads the first entry that it finds that matches the key fields that you have specified.

The result of the selection is either an elementary field or a flat structure, depending on the number of columns you specified in <cols>. The target area in the INTO clause must be appropriately convertible.

If the system finds a line with the corresponding key, SY-SUBRC is set to 0, otherwise to 4.

Select Up to N rows - is basically used for restricting the no. of. lines included in the selection. If n is a positive integer, the system reads a max of n rows. If n is zero, the system reads all the lines that meet the seletion criteria.

Regards,

JLN