21.10.2013 Views

Firebird 2.1 Language Reference Update

Firebird 2.1 Language Reference Update

Firebird 2.1 Language Reference Update

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ROWS [TO ]<br />

DML statements<br />

::= The usual output column specifications.<br />

, ::= Any expression evaluating to an integer.<br />

With a UNION:<br />

SELECT [FIRST p] [SKIP q] FROM ...<br />

[WHERE ...]<br />

[ORDER BY ...]<br />

UNION [ALL | DISTINCT]<br />

SELECT [FIRST r] [SKIP s] FROM ...<br />

[WHERE ...]<br />

[ORDER BY ...]<br />

ROWS [TO ]<br />

With a single argument m, the first m rows of the dataset are returned.<br />

Points to note:<br />

• If m > the total number of rows in the dataset, the entire set is returned.<br />

• If m = 0, an empty set is returned.<br />

• If m < 0, an error is raised.<br />

With two arguments m and n, rows m to n of the dataset are returned, inclusively. Row numbers are 1-based.<br />

Points to note when using two arguments:<br />

• If m > the total number of rows in the dataset, an empty set is returned.<br />

• If m lies within the set but n doesn't, the rows from m to the end of the set are returned.<br />

• If m < 1 or n < 1, an error is raised.<br />

• If n = m-1, an empty set is returned.<br />

• If n < m-1, an error is raised.<br />

The SQL-compliant ROWS syntax obviates the need for FIRST and SKIP, except in one case: a SKIP without<br />

FIRST, which returns the entire remainder of the set after skipping a given number of rows. (You can often “fake<br />

it” though, by supplying a second argument that you know to be bigger than the number of rows in the set.)<br />

You cannot use ROWS together with FIRST and/or SKIP in a single SELECT statement, but is it valid to use one<br />

form in the top-level statement and the other in subselects, or to use the two syntaxes in different subselects.<br />

When used with a UNION, the ROWS subclause applies to the UNION as a whole and must be placed after<br />

the last SELECT. If you want to limit the output of one or more individual SELECTs within the UNION, you<br />

have two options: either use FIRST/SKIP on those SELECT statements, or convert them to derived tables with<br />

ROWS clauses.<br />

ROWS can also be used with the UPDATE and DELETE statements.<br />

UNION<br />

Available in: DSQL, ESQL, PSQL<br />

82

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!