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

Ulrich Scholz picture Ulrich Scholz · Dec 21, 2015 · Viewed 10.7k times · Source

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?

Answer

Grigory Loskutov picture Grigory Loskutov · Dec 21, 2015

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.