|
1 | 1 | <!--
|
2 |
| -$PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.106 2008/10/07 19:27:04 tgl Exp $ |
| 2 | +$PostgreSQL: pgsql/doc/src/sgml/ref/select.sgml,v 1.107 2008/10/22 11:00:33 petere Exp $ |
3 | 3 | PostgreSQL documentation
|
4 | 4 | -->
|
5 | 5 |
|
@@ -30,7 +30,8 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replac
|
30 | 30 | [ { UNION | INTERSECT | EXCEPT } [ ALL ] <replaceable class="parameter">select</replaceable> ]
|
31 | 31 | [ ORDER BY <replaceable class="parameter">expression</replaceable> [ ASC | DESC | USING <replaceable class="parameter">operator</replaceable> ] [ NULLS { FIRST | LAST } ] [, ...] ]
|
32 | 32 | [ LIMIT { <replaceable class="parameter">count</replaceable> | ALL } ]
|
33 |
| - [ OFFSET <replaceable class="parameter">start</replaceable> ] |
| 33 | + [ OFFSET <replaceable class="parameter">start</replaceable> [ ROW | ROWS ] ] |
| 34 | + [ FETCH { FIRST | NEXT } [ <replaceable class="parameter">count</replaceable> ] { ROW | ROWS } ONLY ] |
34 | 35 | [ FOR { UPDATE | SHARE } [ OF <replaceable class="parameter">table_name</replaceable> [, ...] ] [ NOWAIT ] [...] ]
|
35 | 36 |
|
36 | 37 | where <replaceable class="parameter">from_item</replaceable> can be one of:
|
@@ -150,7 +151,7 @@ and <replaceable class="parameter">with_query</replaceable> is:
|
150 | 151 |
|
151 | 152 | <listitem>
|
152 | 153 | <para>
|
153 |
| - If the <literal>LIMIT</literal> or <literal>OFFSET</literal> |
| 154 | + If the <literal>LIMIT</literal> (or <literal>FETCH FIRST</literal>) or <literal>OFFSET</literal> |
154 | 155 | clause is specified, the <command>SELECT</command> statement
|
155 | 156 | only returns a subset of the result rows. (See <xref
|
156 | 157 | linkend="sql-limit" endterm="sql-limit-title"> below.)
|
@@ -891,6 +892,24 @@ OFFSET <replaceable class="parameter">start</replaceable>
|
891 | 892 | class="parameter">count</replaceable> rows to be returned.
|
892 | 893 | </para>
|
893 | 894 |
|
| 895 | + <para> |
| 896 | + SQL:2008 introduced a different syntax to achieve the same thing, |
| 897 | + which PostgreSQL also supports. It is: |
| 898 | +<synopsis> |
| 899 | +OFFSET <replaceable class="parameter">start</replaceable> { ROW | ROWS } |
| 900 | +FETCH { FIRST | NEXT } [ <replaceable class="parameter">count</replaceable> ] { ROW | ROWS } ONLY |
| 901 | +</synopsis> |
| 902 | + Both clauses are optional, but if present |
| 903 | + the <literal>OFFSET</literal> clause must come before |
| 904 | + the <literal>FETCH</literal> clause. <literal>ROW</literal> |
| 905 | + and <literal>ROWS</literal> as well as <literal>FIRST</literal> |
| 906 | + and <literal>NEXT</literal> are noise words that don't influence |
| 907 | + the effects of these clauses. When using expressions other than |
| 908 | + constants for the offset or fetch count, parentheses will be |
| 909 | + necessary in most cases. If the fetch count is omitted, it |
| 910 | + defaults to 1. |
| 911 | + </para> |
| 912 | + |
894 | 913 | <para>
|
895 | 914 | When using <literal>LIMIT</>, it is a good idea to use an
|
896 | 915 | <literal>ORDER BY</> clause that constrains the result rows into a
|
@@ -1337,13 +1356,30 @@ SELECT distributors.* WHERE distributors.name = 'Westward';
|
1337 | 1356 | </para>
|
1338 | 1357 | </refsect2>
|
1339 | 1358 |
|
| 1359 | + <refsect2> |
| 1360 | + <title><literal>LIMIT</literal> and <literal>OFFSET</literal></title> |
| 1361 | + |
| 1362 | + <para> |
| 1363 | + The clauses <literal>LIMIT</literal> and <literal>OFFSET</literal> |
| 1364 | + are <productname>PostgreSQL</productname>-specific syntax, also |
| 1365 | + used by <productname>MySQL</productname>. The SQL:2008 standard |
| 1366 | + has introduced the clauses <literal>OFFSET ... FETCH {FIRST|NEXT} |
| 1367 | + ...</literal> for the same functionality, as shown above |
| 1368 | + in <xref linkend="sql-limit" endterm="sql-limit-title">, and this |
| 1369 | + syntax is also used by <productname>IBM DB2</productname>. |
| 1370 | + (Applications written for <productname>Oracle</productname> |
| 1371 | + frequently use a workaround involving the automatically |
| 1372 | + generated <literal>rownum</literal> column, not available in |
| 1373 | + PostgreSQL, to implement the effects of these clauses.) |
| 1374 | + </para> |
| 1375 | + </refsect2> |
| 1376 | + |
1340 | 1377 | <refsect2>
|
1341 | 1378 | <title>Nonstandard Clauses</title>
|
1342 | 1379 |
|
1343 | 1380 | <para>
|
1344 |
| - The clauses <literal>DISTINCT ON</literal>, |
1345 |
| - <literal>LIMIT</literal>, and <literal>OFFSET</literal> are not |
1346 |
| - defined in the SQL standard. |
| 1381 | + The clause <literal>DISTINCT ON</literal> is not defined in the |
| 1382 | + SQL standard. |
1347 | 1383 | </para>
|
1348 | 1384 | </refsect2>
|
1349 | 1385 | </refsect1>
|
|
0 commit comments