SAP NetWeaver AS ABAP Release 752, ©Copyright 2017 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP - Reference → Processing External Data → ABAP Database Accesses → Open SQL → Open SQL - Reads → SELECT clauses →
SELECT - UP TO, OFFSET
Syntax
... [UP TO n ROWS]
[OFFSET o] ...
Extras:
1.... UP TO n ROWS
2.... OFFSET o
Effect
These optional additions of a query of a SELECT statement or WITH statement restrict the results set using an offset and the maximum number of rows read. The syntax varies as follows for main queries and subqueries:
Note
These additions are applied to the results set defined by the preceding clauses.
... UP TO n ROWS
Effect
The addition UP TO limits the number of rows in the results set of a SELECT statement to n. n expects a host variable prefixed by an escape character @, a host expression or literal of type i that can represent all non-negative numbers from the value range of i except its maximum value +2,147,483,647. In the strict mode from Release 7.51, n must have the types b, s, i, or int8. Furthermore, a literal or constant specified for n cannot have the value 0 in this strict mode. The content of n must match the data type i in accordance with the rules for a lossless assignment. A host variable should be prefixed by the escape character @.
The addition UP TO cannot be used with addition SINGLE and cannot be used with UNION.
Notes
Example
Reads the three business customers with the highest discount rates:
SELECT *
FROM scustom
WHERE custtype = 'B'
ORDER BY discount DESCENDING
INTO TABLE @DATA(result)
UP TO 3 ROWS.
... OFFSET o
Effect
The addition OFFSET is used to return only the rows after the row with the count o from the results set. If OFFSET is specified, the results set must be sorted using ORDER BY. o expects a host variable prefixed by an escape character @, a host expression, or a literal of the type b, s, i or int8 that can represent all non-negative numbers in the value range of i except its maximum value +2,147,483,647. A literal or constant specified for n cannot have the value 0. A host variable must be prefixed by the escape character @.
The addition OFFSET cannot be used together with the additions
SINGLE and FOR
ALL ENTRIES, not when UNION is used and not when
pooled tables/
cluster tables or
projection views are accessed.
Note
When the addition OFFSET is used, the syntax check is performed in a
strict mode, which handles the statement more strictly than the regular syntax check.
Example
Reads the data of all flights of a connection (except for the ten flights with the fewest seats taken).
SELECT fldate
FROM sflight
WHERE carrid = 'LH' AND connid = '400'
ORDER BY seatsocc ASCENDING, fldate
INTO TABLE @DATA(result)
OFFSET 10.
Executable Example
SELECT, Restriction of the Rows in the Results Set