From 361bfc35724aac207f7a013336e058704fbadf60 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 22 Oct 2008 11:00:34 +0000 Subject: SQL:2008 alternative syntax for LIMIT/OFFSET: OFFSET num {ROW|ROWS} FETCH {FIRST|NEXT} [num] {ROW|ROWS} ONLY --- doc/src/sgml/ref/select.sgml | 48 ++++++++++++++++++++++++++++++++++----- doc/src/sgml/ref/select_into.sgml | 5 ++-- doc/src/sgml/ref/values.sgml | 16 ++++++++----- 3 files changed, 55 insertions(+), 14 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index f73ca6ed64a..91a756b3ae2 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1,5 +1,5 @@ @@ -30,7 +30,8 @@ SELECT [ ALL | DISTINCT [ ON ( expressionselect ] [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] [ LIMIT { count | ALL } ] - [ OFFSET start ] + [ OFFSET start [ ROW | ROWS ] ] + [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ] [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ] where from_item can be one of: @@ -150,7 +151,7 @@ and with_query is: - If the LIMIT or OFFSET + If the LIMIT (or FETCH FIRST) or OFFSET clause is specified, the SELECT statement only returns a subset of the result rows. (See below.) @@ -891,6 +892,24 @@ OFFSET start class="parameter">count rows to be returned. + + SQL:2008 introduced a different syntax to achieve the same thing, + which PostgreSQL also supports. It is: + +OFFSET start { ROW | ROWS } +FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY + + Both clauses are optional, but if present + the OFFSET clause must come before + the FETCH clause. ROW + and ROWS as well as FIRST + and NEXT are noise words that don't influence + the effects of these clauses. When using expressions other than + constants for the offset or fetch count, parentheses will be + necessary in most cases. If the fetch count is omitted, it + defaults to 1. + + When using LIMIT, it is a good idea to use an ORDER BY clause that constrains the result rows into a @@ -1337,13 +1356,30 @@ SELECT distributors.* WHERE distributors.name = 'Westward'; + + <literal>LIMIT</literal> and <literal>OFFSET</literal> + + + The clauses LIMIT and OFFSET + are PostgreSQL-specific syntax, also + used by MySQL. The SQL:2008 standard + has introduced the clauses OFFSET ... FETCH {FIRST|NEXT} + ... for the same functionality, as shown above + in , and this + syntax is also used by IBM DB2. + (Applications written for Oracle + frequently use a workaround involving the automatically + generated rownum column, not available in + PostgreSQL, to implement the effects of these clauses.) + + + Nonstandard Clauses - The clauses DISTINCT ON, - LIMIT, and OFFSET are not - defined in the SQL standard. + The clause DISTINCT ON is not defined in the + SQL standard. diff --git a/doc/src/sgml/ref/select_into.sgml b/doc/src/sgml/ref/select_into.sgml index de9a86a878c..cbe46cf763e 100644 --- a/doc/src/sgml/ref/select_into.sgml +++ b/doc/src/sgml/ref/select_into.sgml @@ -1,5 +1,5 @@ @@ -31,7 +31,8 @@ SELECT [ ALL | DISTINCT [ ON ( expressionselect ] [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] [ LIMIT { count | ALL } ] - [ OFFSET start ] + [ OFFSET start [ ROW | ROWS ] ] + [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ] [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ] diff --git a/doc/src/sgml/ref/values.sgml b/doc/src/sgml/ref/values.sgml index 23b84d78203..ef605f818f0 100644 --- a/doc/src/sgml/ref/values.sgml +++ b/doc/src/sgml/ref/values.sgml @@ -1,5 +1,5 @@ @@ -23,7 +23,8 @@ PostgreSQL documentation VALUES ( expression [, ...] ) [, ...] [ ORDER BY sort_expression [ ASC | DESC | USING operator ] [, ...] ] [ LIMIT { count | ALL } ] - [ OFFSET start ] + [ OFFSET start [ ROW | ROWS ] ] + [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ] @@ -48,8 +49,10 @@ VALUES ( expression [, ...] ) [, .. Within larger commands, VALUES is syntactically allowed anywhere that SELECT is. Because it is treated like a - SELECT by the grammar, it is possible to use the ORDER - BY, LIMIT, and OFFSET clauses with a + SELECT by the grammar, it is possible to use + the ORDER BY, LIMIT (or + equivalently FETCH FIRST), + and OFFSET clauses with a VALUES command. @@ -227,9 +230,10 @@ WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1. Compatibility - VALUES conforms to the SQL standard, except that + VALUES conforms to the SQL standard. LIMIT and OFFSET are - PostgreSQL extensions. + PostgreSQL extensions; see also + under . -- cgit v1.2.3