~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to doc/src/sgml/ref/select_into.sgml

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!--
 
2
$PostgreSQL: pgsql/doc/src/sgml/ref/select_into.sgml,v 1.33 2005-01-09 05:57:45 tgl Exp $
 
3
PostgreSQL documentation
 
4
-->
 
5
 
 
6
<refentry id="SQL-SELECTINTO">
 
7
 <refmeta>
 
8
  <refentrytitle id="SQL-SELECTINTO-TITLE">SELECT INTO</refentrytitle>
 
9
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 
10
 </refmeta>
 
11
 
 
12
 <refnamediv>
 
13
  <refname>SELECT INTO</refname>
 
14
  <refpurpose>define a new table from the results of a query</refpurpose>
 
15
 </refnamediv>
 
16
 
 
17
 <indexterm zone="sql-selectinto">
 
18
  <primary>SELECT INTO</primary>
 
19
 </indexterm>
 
20
 
 
21
 <refsynopsisdiv>
 
22
<synopsis>
 
23
SELECT [ ALL | DISTINCT [ ON ( <replaceable class="PARAMETER">expression</replaceable> [, ...] ) ] ]
 
24
    * | <replaceable class="PARAMETER">expression</replaceable> [ AS <replaceable class="PARAMETER">output_name</replaceable> ] [, ...]
 
25
    INTO [ TEMPORARY | TEMP ] [ TABLE ] <replaceable class="PARAMETER">new_table</replaceable>
 
26
    [ FROM <replaceable class="PARAMETER">from_item</replaceable> [, ...] ]
 
27
    [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
 
28
    [ GROUP BY <replaceable class="PARAMETER">expression</replaceable> [, ...] ]
 
29
    [ HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
 
30
    [ { UNION | INTERSECT | EXCEPT } [ ALL ] <replaceable class="PARAMETER">select</replaceable> ]
 
31
    [ ORDER BY <replaceable class="PARAMETER">expression</replaceable> [ ASC | DESC | USING <replaceable class="PARAMETER">operator</replaceable> ] [, ...] ]
 
32
    [ LIMIT { <replaceable class="PARAMETER">count</replaceable> | ALL } ]
 
33
    [ OFFSET <replaceable class="PARAMETER">start</replaceable> ]
 
34
    [ FOR UPDATE [ OF <replaceable class="PARAMETER">tablename</replaceable> [, ...] ] ]
 
35
</synopsis>
 
36
 </refsynopsisdiv>
 
37
 
 
38
 <refsect1>
 
39
  <title>Description</title>
 
40
 
 
41
  <para>
 
42
   <command>SELECT INTO</command> creates a new table and fills it
 
43
   with data computed by a query.  The data is not returned to the
 
44
   client, as it is with a normal <command>SELECT</command>.  The new
 
45
   table's columns have the names and data types associated with the
 
46
   output columns of the <command>SELECT</command>.
 
47
  </para>
 
48
 </refsect1>
 
49
  
 
50
 <refsect1>
 
51
  <title>Parameters</title>
 
52
 
 
53
  <variablelist>
 
54
  <varlistentry>
 
55
   <term><literal>TEMPORARY</literal> or <literal>TEMP</literal></term>
 
56
   <listitem>
 
57
    <para>
 
58
     If specified, the table is created as a temporary table.  Refer
 
59
     to <xref linkend="sql-createtable"
 
60
     endterm="sql-createtable-title"> for details.
 
61
    </para>
 
62
   </listitem>
 
63
  </varlistentry>
 
64
 
 
65
   <varlistentry>
 
66
    <term><replaceable class="PARAMETER">new_table</replaceable></term>
 
67
    <listitem>
 
68
     <para>
 
69
      The name (optionally schema-qualified) of the table to be created.
 
70
     </para>
 
71
    </listitem>
 
72
   </varlistentry>
 
73
  </variablelist>
 
74
 
 
75
  <para>
 
76
   All other parameters are described in detail under <xref
 
77
   linkend="sql-select" endterm="sql-select-title">.
 
78
  </para>
 
79
 </refsect1>
 
80
 
 
81
 <refsect1>
 
82
  <title>Notes</title>
 
83
 
 
84
  <para>
 
85
   <xref linkend="sql-createtableas"
 
86
   endterm="sql-createtableas-title"> is functionally similar to
 
87
   <command>SELECT INTO</command>.  <command>CREATE TABLE AS</command>
 
88
   is the recommended syntax, since this form of <command>SELECT
 
89
   INTO</command> is not available in <application>ECPG</application>
 
90
   or <application>PL/pgSQL</application>, because they interpret the
 
91
   <literal>INTO</literal> clause differently. Furthermore,
 
92
   <command>CREATE TABLE AS</command> offers a superset of the
 
93
   functionality provided by <command>SELECT INTO</command>.
 
94
  </para>
 
95
 
 
96
  <para>
 
97
   Prior to <productname>PostgreSQL</> 8.0, the table created by
 
98
   <command>SELECT INTO</command> always included OIDs.
 
99
   As of <productname>PostgreSQL</> 8.0, the
 
100
   inclusion of OIDs in the table created by <command>SELECT
 
101
   INTO</command> is controlled by the
 
102
   <xref linkend="guc-default-with-oids"> configuration variable. This
 
103
   variable currently defaults to true, but will likely default to
 
104
   false in a future release of <productname>PostgreSQL</>.
 
105
  </para>
 
106
 </refsect1>
 
107
 
 
108
 <refsect1>
 
109
  <title>Examples</title>
 
110
 
 
111
  <para>
 
112
   Create a new table <literal>films_recent</literal> consisting of only
 
113
   recent entries from the table <literal>films</literal>:
 
114
 
 
115
<programlisting>
 
116
SELECT * INTO films_recent FROM films WHERE date_prod &gt;= '2002-01-01';
 
117
</programlisting>
 
118
  </para>
 
119
 </refsect1>
 
120
 
 
121
 <refsect1>
 
122
  <title>Compatibility</title>
 
123
 
 
124
  <para>
 
125
   The SQL standard uses <command>SELECT INTO</command> to
 
126
   represent selecting values into scalar variables of a host program,
 
127
   rather than creating a new table.  This indeed is the usage found
 
128
   in <application>ECPG</application> (see <xref linkend="ecpg">) and
 
129
   <application>PL/pgSQL</application> (see <xref linkend="plpgsql">).
 
130
   The <productname>PostgreSQL</productname> usage of <command>SELECT
 
131
   INTO</command> to represent table creation is historical.  It is
 
132
   best to use <command>CREATE TABLE AS</command> for this purpose in
 
133
   new code.
 
134
  </para>
 
135
 </refsect1>
 
136
 
 
137
 <refsect1>
 
138
  <title>See Also</title>
 
139
 
 
140
  <simplelist type="inline">
 
141
   <member><xref linkend="sql-createtableas" endterm="sql-createtableas-title"></member>
 
142
  </simplelist>
 
143
 </refsect1>
 
144
</refentry>
 
145
 
 
146
<!-- Keep this comment at the end of the file
 
147
Local variables:
 
148
mode: sgml
 
149
sgml-omittag:nil
 
150
sgml-shorttag:t
 
151
sgml-minimize-attributes:nil
 
152
sgml-always-quote-attributes:t
 
153
sgml-indent-step:1
 
154
sgml-indent-data:t
 
155
sgml-parent-document:nil
 
156
sgml-default-dtd-file:"../reference.ced"
 
157
sgml-exposed-tags:nil
 
158
sgml-local-catalogs:"/usr/lib/sgml/catalog"
 
159
sgml-local-ecat-files:nil
 
160
End:
 
161
-->