ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing External Data →  ABAP Database Access →  ABAP SQL →  ABAP SQL - Read Access →  SELECT, clauses →  SELECT, abap_options → 
Mail Feedback

SELECT, UP TO, OFFSET

Short Reference

Syntax

... [UP TO n ROWS]
    [OFFSET o] ...

Additions:

1.... UP TO n ROWS

2.... OFFSET o

Effect

These optional additions of a query of a SELECT statement or WITH statement restrict the result set using an offset and the maximum number of rows to be read. The syntax varies as follows for main queries and subqueries:

Hint

These additions affect the result set defined by the preceding clauses.

Addition 1  

... UP TO n ROWS

Effect

The addition UP TO limits the number of rows in the result set of a SELECT statement to n. For n, a host variable, a host expression, or a literal of type i is expected, which can represent all non-negative numbers from the value range of i except its maximum value +2,147,483,647. Only the types b, s, i, or int8 can be specified for n. Furthermore, a literal or constant specified for n cannot have the value 0. This is checked in strict mode from ABAP release 7.51. If n is an untyped literal or a host variable, its content must match the data type i in accordance with the rules for a lossless assignment.

The addition UP TO cannot be used with addition SINGLE and cannot be used with UNION, INTERSECT, or EXCEPT.

Hints

Example

Reading of the three business customers with the highest discount rates.

SELECT *
       FROM scustom
       WHERE custtype = 'B'
       ORDER BY discount DESCENDING
       INTO TABLE @FINAL(result)
       UP TO 3 ROWS.

Addition 2  

... OFFSET o

Effect

The addition OFFSET is used to return only the rows after the row with the count o from the result set. If OFFSET is specified, the result set must be sorted using ORDER BY. o expects a host variable, a host expression, or a literal of the type b, s, i, or int8, which 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.

The addition OFFSET cannot be used together with the additions SINGLE and FOR ALL ENTRIES, and not when UNION, INTERSECT, or EXCEPT is used, and not when DDIC projection views are accessed.

Hints



Example

Reading of 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 @FINAL(result)
       OFFSET 10.

Executable Example

Restricting the result set



Continue
Example SELECT, Restriction of the Rows in the Result Set