What's the difference?
A lot of people use the select single statement to check for the existence of a value in a database prior to running a large report. 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.
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
Code:
SELECT SINGLE field INTO w_field FROM table.
And
Code:
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 or lack of, applies any aggregate, ordering or grouping functions to them and then returns the first record of the resultant result set.
Get the difference ??
If not, then 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 shoshould see the output:
Code:
Select single: 000001Up to 1 rows : 000010
The first 'select' statement has selected the first record in the database according to any selection criteria in the 'where' clause. this is what a 'select singles' 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 database has read the entire table, sort it and then return the first record. if there was no order by clause then the results wowould have been identical (I. e. both '200') but the second select if given a big enough table to look at wocould be far slower.
Now.
This causes a problem in the extended program check in that if the full key is not specified in a 'select single 'you get a message like this:
Quote:
Program: z_difference line: 39 Syntax check warning This warning is only displayed in slin. Select single posnr ^ Messages: In "select single...", the where Condition for the key field "posnr" does not test For equality. Therefore, the single record you are searching for may not be Unique. |
If you haven't specified a full key and your QA person is complaining that your extended check has warnings tell him
"Yes. I can get rid of the warning but the program will run slower and consume more memory ."
You coshould always tell him to "get lost" but it's always better to have a valid reason before you do that!
Having said that, not long after I posted this article, christianf sent me a text.
The basic gist of this was that the comparison between the two select statements above is unfair.
"In both cases, the way the record is found on the database level is the same and in both cases the number of rows to be selected is known at the database level. therefore in both cases the database stops after the first found record"
Christian says that the described behaviour or at least a similar behaviour can be watched on the following statement:
Code:
SELECT field INTO w_field FROM table.Exit.ENDSELECT.
He goes on to say:
"Here the result of the database is transferred back in 30 K blocks and then transferred to the Application Server linewise. so even for larger tables at least 10 or more records are transferred (and maybe even more searched on a database level) Where only one is required."
Another point raised is that you cannot use aggregations in a select single statement, and the "order" clause is causing them to act differently. I wocould say that this article is attempting to answer the question "What is the difference between select single and up to 1 rows", therefore I think these pertinent.
Thanks to Christian for a lower level insight into the workings of select up to 1 rows!