ABAP 7.40 SELECT .. ENDSELECT UP TO n ROWS syntax?

11,631

Solution 1

I can't check it right now, but I suspect that you have "INTO ..." and "UP TO ..." parts placed after "WHERE ..." and "ORDER BY ..." parts. Documentation states that the syntax of SELECT is:

SELECT result 
 INTO target
 FROM source 
 [WHERE condition]
 [GROUP BY fields] 
 [HAVING  cond]
 [ORDER BY fields].

with remark "The FROM clause ... can also be placed before the INTO clause." There are no remarks that you can insert WHERE/GROUP BY/HAVING/ORDER BY between SELECT/INTO/FROM.

So, give a try to:

SELECT *
   FROM scustom
   INTO @wa_scustom
   UP TO 3 ROWS
   WHERE custtype = 'B'
   ORDER BY discount DESCENDING.
ENDSELECT. 

Solution 2

Indeed, there's a syntax imposed by SAP that only allows "UP TO n ROWS" and other arguments to come BEFORE the WHERE clause. However, there are more flexibility to it in newer ABAP server releases.

When generalizing, please use the older syntax. It will still work on newer versions, as SAP has a strong backward compatibility policy.

"Be conservative in what you send, be liberal in what you accept".

Something like that:

SELECT {fields}
  FROM {db table}
  INTO {work area}
  UP TO {n} ROWS
  WHERE {field} {condition} {value}
  ORDER BY {field} {ASCENDING/DESCENDING}.
ENDSELECT.

Hope it helps.

Share:
11,631
Ulrich Scholz
Author by

Ulrich Scholz

Currently working at smartShift Technologies in SAP legacy transformation. We build a rule based tool in Java that automatically transforms SAP systems to be more comformant, robust, secure, fast, and HANA-complient. The transformation is repeatable, tractable, and correct. We also offer reports on various system parameters, like code quality.

Updated on June 04, 2022

Comments

  • Ulrich Scholz
    Ulrich Scholz almost 2 years

    Update: The question should be withdrawn, the grammar is correct. Apparently, SAP defines ABAP via a grammar, which is then modified by additional rules in plain text. I missed this second part.


    I'm looking at the ABAP Keyword Documentation 7.40, SELECT -> SELECT additions. For addition UP TO n ROWS, it gives the example

    DATA: wa_scustom TYPE scustom.
    
    SELECT *
           FROM scustom
           WHERE custtype = 'B'
           ORDER BY discount DESCENDING
           INTO @wa_scustom
           UP TO 3 ROWS.
    ENDSELECT. 
    

    I verified that code in an SAP 7.40 system and got the error

    Row 7: "INTO" is not valid here. '.' is expected

    On the other hand, the following code is accepted, although it is not covered by the grammar of SELECT as given in the document: UP TO n ROWS should be after the FROM.

    SELECT COUNT(*) UP TO 1 ROWS
        FROM MARC
        WHERE matnr eq 100.
    

    As we are writing a tool that automatically generates ABAP code, it would be nice to know what's legal and what's not. Is there a "definitive" document? In general, is it worth the try to contact someone at SAP for corrections? (You see, I'm somewhat alien to the SAP world) If yes, who could that be?