메뉴 건너뛰기

SAP 한국 커뮤니티

Difference between select single and up to 1 row

sapjoy 2006.12.12 21:08 조회 수 : 6810 추천:45

Difference between select single and up to 1 row
Can any one tell what is the difference between select single * from kna1 and select * from kna1 up to 1 row.

Raghavender
  

Its reallly good question:
Select single * from KNA1 where clause
---It fetches single record from the database, based on the condition you specified in the where clause.

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.

Janadhan
  

Whats The Difference. SELECT SINGLE or UP TO 1 ROWS, Whats 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 memeory 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 should see the output:
Code:

Select single: 000001
Up 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 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.

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 could always tell him to "Get Lost" but it's always better to have a valid reason before you do that!
번호 제목 글쓴이 날짜 조회 수
407 ICON으로 DISPLAY sapjoy 2007.08.06 4028
406 너무도 당연해서 가끔 잊곤하는 공기와 같은 존재 'F1' key [5] 백혜정 2007.09.19 4034
405 Widget 한번 개발해 보세요.. [11] 해처리 2008.04.17 4038
404 interanl table header line [1] sapjoy 2006.12.05 4040
403 설명이 비교적 자세하고 원리를 알수 있는 효과적인 ABAP코딩 문서.doc [13] file 박영신 2007.07.26 4041
402 테이블 데이터 엑셀 업로드 프로그램 sapjoy 2014.06.17 4055
401 Lock objects [3] sapjoy 2006.12.20 4058
400 SAP Service Marketplace, SDN에 클릭한번으로 로그인 하는 방법 [2] sapjoy 2007.05.25 4059
399 DUMP : CONNE_IMPORT_WRONG_STRUCTURE INDX(KE) sapjoy 2015.03.18 4073
398 심플한 Progress 올려봅니다. [2] 김지성 2007.06.19 4074
397 아밥에서 다운받을시 로컬피씨에 폴더 중복 방지 소스 [3] file 양제부 2007.05.16 4086
396 Adding custom fields to CS01, CS02 and CS03 (Bill Of Materials) [1] file 노름마치 2009.07.10 4087
395 Debug program running in background [1] sapjoy 2006.12.08 4091
394 SAP R/3가 지원하는 제조전략 명신용 2007.04.03 4094
393 sap에서 메일 보내고 받기 소스와 예제 설명입니다. [7] file 박.. 2007.05.19 4094
392 Implementing Screen-exit for transaction MIGO [5] file 노름마치 2009.07.10 4119
391 ABAP에서 .Net connector로 .Net Function호출 시 한글이 깨질 경우 홍성현 2007.08.08 4120
390 간단팁2 : 특정 값으로 구분된 텍스트, 한방에 분리 및 각각의 변수에 저장 [2] 윤훈태 2007.10.15 4139
389 saplogon 할때 갑자기 윈도우 services화일 못찾을떄 요섭아부지 2008.06.12 4144
388 SAT 사용법 DEMO 영상 [3] sapjoy 2015.01.21 4150