Skip to Content
author's profile photo Former Member
Former Member

select

deferance bw selectupto 1row

select single

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

8 Answers

  • author's profile photo Former Member
    Former Member
    Posted on May 08, 2007 at 12:43 PM

    Hi,

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

Add a comment
10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 08, 2007 at 12:41 PM

    Hi

    Difference Between Select Single and Select UpTo One Rows

    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.

    Reward points if useful

    Regards

    Anji

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 08, 2007 at 12:42 PM

    if u have all key fields with u then select single can be used which will fetch only 1 record.

    but if u dont have all key fields and if u use select statement and multiple entries might get fetched then u use select up to 1 rows which will fetch 1 record.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 08, 2007 at 12:45 PM

    Hi Rajamahender,

    Difference Between Select Single and Select UpTo One Rows

    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.

    Reward points if helpful.

    Regards,

    Hemant

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 08, 2007 at 01:03 PM

    select single will get all records from database table and display only one record

    select up to rows will get only specified records from database tables and it displays that records.

    reward some points if helpful.

    with regards,

    suresh babu.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 26, 2007 at 06:07 AM

    SELECT SINGLE will get only specified records from database tables and it displays that records.all records from database table and display only one record

    SELECT UP TO n ROWS will get all records from database table and display only one record.

    Difference will also be in Performance times.

    Eg::

    data: m1 like t006 occurs 0 with header line.

    Select * from t006 into table m1

    <b>up to 1 rows</b>

    where MSEHI eq '001'.

    <b>Runtime: 10 microseconds</b>

    data: m2 like t006 occurs 0 with header line.

    <b>Select single</b> * from t006 into m2

    where MSEHI eq '001'.

    <b>Runtime: 7 microseconds</b>

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 26, 2007 at 06:15 AM

    hi

    Difference Between Select Single and Select UpTo One Rows

    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.

    Reward if usefull

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 26, 2007 at 06:15 AM

    Hi

    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.

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

    Reward if usefull

    Add a comment
    10|10000 characters needed characters exceeded

    Before answering

    You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
    You must be Logged in to submit an answer.

    Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.