gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 line
8.2KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>SELECT INTO</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-select.html" title="SELECT" /><link rel="next" href="sql-set.html" title="SET" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">SELECT INTO</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-select.html" title="SELECT">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-set.html" title="SET">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-SELECTINTO"><div class="titlepage"></div><a id="id-1.9.3.172.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SELECT INTO</span></h2><p>SELECT INTO — define a new table from the results of a query</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. [ WITH [ RECURSIVE ] <em class="replaceable"><code>with_query</code></em> [, ...] ]
  4. SELECT [ ALL | DISTINCT [ ON ( <em class="replaceable"><code>expression</code></em> [, ...] ) ] ]
  5. * | <em class="replaceable"><code>expression</code></em> [ [ AS ] <em class="replaceable"><code>output_name</code></em> ] [, ...]
  6. INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] <em class="replaceable"><code>new_table</code></em>
  7. [ FROM <em class="replaceable"><code>from_item</code></em> [, ...] ]
  8. [ WHERE <em class="replaceable"><code>condition</code></em> ]
  9. [ GROUP BY <em class="replaceable"><code>expression</code></em> [, ...] ]
  10. [ HAVING <em class="replaceable"><code>condition</code></em> [, ...] ]
  11. [ WINDOW <em class="replaceable"><code>window_name</code></em> AS ( <em class="replaceable"><code>window_definition</code></em> ) [, ...] ]
  12. [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] <em class="replaceable"><code>select</code></em> ]
  13. [ ORDER BY <em class="replaceable"><code>expression</code></em> [ ASC | DESC | USING <em class="replaceable"><code>operator</code></em> ] [ NULLS { FIRST | LAST } ] [, ...] ]
  14. [ LIMIT { <em class="replaceable"><code>count</code></em> | ALL } ]
  15. [ OFFSET <em class="replaceable"><code>start</code></em> [ ROW | ROWS ] ]
  16. [ FETCH { FIRST | NEXT } [ <em class="replaceable"><code>count</code></em> ] { ROW | ROWS } ONLY ]
  17. [ FOR { UPDATE | SHARE } [ OF <em class="replaceable"><code>table_name</code></em> [, ...] ] [ NOWAIT ] [...] ]
  18. </pre></div><div class="refsect1" id="id-1.9.3.172.5"><h2>Description</h2><p>
  19. <code class="command">SELECT INTO</code> creates a new table and fills it
  20. with data computed by a query. The data is not returned to the
  21. client, as it is with a normal <code class="command">SELECT</code>. The new
  22. table's columns have the names and data types associated with the
  23. output columns of the <code class="command">SELECT</code>.
  24. </p></div><div class="refsect1" id="id-1.9.3.172.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">TEMPORARY</code> or <code class="literal">TEMP</code></span></dt><dd><p>
  25. If specified, the table is created as a temporary table. Refer
  26. to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for details.
  27. </p></dd><dt><span class="term"><code class="literal">UNLOGGED</code></span></dt><dd><p>
  28. If specified, the table is created as an unlogged table. Refer
  29. to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for details.
  30. </p></dd><dt><span class="term"><em class="replaceable"><code>new_table</code></em></span></dt><dd><p>
  31. The name (optionally schema-qualified) of the table to be created.
  32. </p></dd></dl></div><p>
  33. All other parameters are described in detail under <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>.
  34. </p></div><div class="refsect1" id="id-1.9.3.172.7"><h2>Notes</h2><p>
  35. <a class="xref" href="sql-createtableas.html" title="CREATE TABLE AS"><span class="refentrytitle">CREATE TABLE AS</span></a> is functionally similar to
  36. <code class="command">SELECT INTO</code>. <code class="command">CREATE TABLE AS</code>
  37. is the recommended syntax, since this form of <code class="command">SELECT
  38. INTO</code> is not available in <span class="application">ECPG</span>
  39. or <span class="application">PL/pgSQL</span>, because they interpret the
  40. <code class="literal">INTO</code> clause differently. Furthermore,
  41. <code class="command">CREATE TABLE AS</code> offers a superset of the
  42. functionality provided by <code class="command">SELECT INTO</code>.
  43. </p><p>
  44. In contrast to <code class="command">CREATE TABLE AS</code>, <code class="command">SELECT
  45. INTO</code> does not allow to specify properties like a table's access
  46. method with <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-METHOD"><code class="literal">USING <em class="replaceable"><code>method</code></em></code></a> or the table's
  47. tablespace with <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-TABLESPACE"><code class="literal">TABLESPACE <em class="replaceable"><code>tablespace_name</code></em></code></a>. Use <a class="xref" href="sql-createtableas.html" title="CREATE TABLE AS"><span class="refentrytitle">CREATE TABLE AS</span></a> if necessary. Therefore, the default table
  48. access method is chosen for the new table. See <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD">default_table_access_method</a> for more information.
  49. </p></div><div class="refsect1" id="id-1.9.3.172.8"><h2>Examples</h2><p>
  50. Create a new table <code class="literal">films_recent</code> consisting of only
  51. recent entries from the table <code class="literal">films</code>:
  52. </p><pre class="programlisting">
  53. SELECT * INTO films_recent FROM films WHERE date_prod &gt;= '2002-01-01';
  54. </pre></div><div class="refsect1" id="id-1.9.3.172.9"><h2>Compatibility</h2><p>
  55. The SQL standard uses <code class="command">SELECT INTO</code> to
  56. represent selecting values into scalar variables of a host program,
  57. rather than creating a new table. This indeed is the usage found
  58. in <span class="application">ECPG</span> (see <a class="xref" href="ecpg.html" title="Chapter 35. ECPG - Embedded SQL in C">Chapter 35</a>) and
  59. <span class="application">PL/pgSQL</span> (see <a class="xref" href="plpgsql.html" title="Chapter 42. PL/pgSQL - SQL Procedural Language">Chapter 42</a>).
  60. The <span class="productname">PostgreSQL</span> usage of <code class="command">SELECT
  61. INTO</code> to represent table creation is historical. It is
  62. best to use <code class="command">CREATE TABLE AS</code> for this purpose in
  63. new code.
  64. </p></div><div class="refsect1" id="id-1.9.3.172.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createtableas.html" title="CREATE TABLE AS"><span class="refentrytitle">CREATE TABLE AS</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-select.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-set.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SELECT </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SET</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1