Ulrich Scholz
Ulrich Scholz

Reputation: 2111

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

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?

Upvotes: 4

Views: 17832

Answers (2)

Bruno Lucattelli
Bruno Lucattelli

Reputation: 347

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.

Upvotes: 1

Grigory Loskutov
Grigory Loskutov

Reputation: 174

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. 

Upvotes: 3

Related Questions