SET ROWCOUNT
- Last Updated: March 30, 2020
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
Limits the result set to a specified number
of rows. This functionality of restricting the size of the result
set is similar to that of the TOP clause. However,
the TOP clause only applies to queries at the statement
level, while SET ROWCOUNT is a session-wide parameter
and is not statement-specific.
Syntax
|
Parameter
- rowcount_number
-
Specifies the number of rows to be restricted from the result set. This value must be a constant literal.
Notes
- The
default value of the row count
is 0, which means that the number of rows in the result set is not restricted, as shown in the example above. When the row count value is greater than 0, the maximum size of the result set is that of the row count value. - You can change the row count value at any point of time in a
session. The new value affects the subsequent
SELECTstatements and stored procedures in the session. -
SET ROWCOUNTrestricts the result set after all the statement level clauses (such as,OFFSET,FETCH, andTOPclauses) have been applied. This implies that after the restriction ofOFFSET,FETCH, andTOPclauses on the result set,SET ROWCOUNTfurther restricts the result set. - The
SET ROWCOUNTstatement has no effect on the following:- DDL statements such as
CREATE,ALTER, andDROP - DML statements such as
INSERT,DELETE, andUPDATE SHOWstatements- Triggers
- DDL statements such as
- The
SET ROWCOUNTstatement has no effect on the internalSELECTstatements used in stored procedures. However, the overall result set of the stored procedures is restricted bySET ROWCOUNT, as shown in the example above. - The
SET ROWCOUNTstatement affects the overall query result and not the intermediate stages of a query execution as the following:- Intermediate result set in a subquery
- Intermediate result set in views or derived tables
- Intermediate queries used as set operators such as,
UNION,INTERSECT, andMINUS
Examples
SET ROWCOUNT statement
The following examples demonstrate
the SET ROWCOUNT statement:
|
The above query returns the first ten rows in the result set.
SET ROWCOUNT statement with OFFSET and FETCH clauses
The following example demonstrates the SET ROWCOUNT statement
with OFFSET and FETCH clauses
in the SELECT statement:
|
The above query further restricts the result
set, after applying the OFFSET and FETCH clauses,
and returns rows 11 through 15.
SET ROWCOUNT statement
The
following example demonstrates the SET ROWCOUNT statement,
where the row count value is set to 3:
|
The above query returns only the first 3 rows in the result set.
Consider running the same query with the
row count value set to 0 (the default value for SET ROWCOUNT),
as shown below:
|
The above query returns the first 5 rows in the result set without restricting any rows.
SET ROWCOUNT statement in a stored procedure
The following example
demonstrates the SET ROWCOUNT statement in a stored
procedure containing more than one row:
|
The above query returns only the first row in the result set.