]> begriffs open source - ai-pg/blob - full-docs/man7/SELECT_INTO.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / SELECT_INTO.7
1 '\" t
2 .\"     Title: SELECT INTO
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "SELECT INTO" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 SELECT_INTO \- define a new table from the results of a query
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 [ WITH [ RECURSIVE ] \fIwith_query\fR [, \&.\&.\&.] ]
36 SELECT [ ALL | DISTINCT [ ON ( \fIexpression\fR [, \&.\&.\&.] ) ] ]
37     [ { * | \fIexpression\fR [ [ AS ] \fIoutput_name\fR ] } [, \&.\&.\&.] ]
38     INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] \fInew_table\fR
39     [ FROM \fIfrom_item\fR [, \&.\&.\&.] ]
40     [ WHERE \fIcondition\fR ]
41     [ GROUP BY \fIexpression\fR [, \&.\&.\&.] ]
42     [ HAVING \fIcondition\fR ]
43     [ WINDOW \fIwindow_name\fR AS ( \fIwindow_definition\fR ) [, \&.\&.\&.] ]
44     [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] \fIselect\fR ]
45     [ ORDER BY \fIexpression\fR [ ASC | DESC | USING \fIoperator\fR ] [ NULLS { FIRST | LAST } ] [, \&.\&.\&.] ]
46     [ LIMIT { \fIcount\fR | ALL } ]
47     [ OFFSET \fIstart\fR [ ROW | ROWS ] ]
48     [ FETCH { FIRST | NEXT } [ \fIcount\fR ] { ROW | ROWS } ONLY ]
49     [ FOR { UPDATE | SHARE } [ OF \fItable_name\fR [, \&.\&.\&.] ] [ NOWAIT ] [\&.\&.\&.] ]
50 .fi
51 .SH "DESCRIPTION"
52 .PP
53 \fBSELECT INTO\fR
54 creates a new table and fills it with data computed by a query\&. The data is not returned to the client, as it is with a normal
55 \fBSELECT\fR\&. The new table\*(Aqs columns have the names and data types associated with the output columns of the
56 \fBSELECT\fR\&.
57 .SH "PARAMETERS"
58 .PP
59 TEMPORARY or TEMP
60 .RS 4
61 If specified, the table is created as a temporary table\&. Refer to
62 CREATE TABLE (\fBCREATE_TABLE\fR(7))
63 for details\&.
64 .RE
65 .PP
66 UNLOGGED
67 .RS 4
68 If specified, the table is created as an unlogged table\&. Refer to
69 CREATE TABLE (\fBCREATE_TABLE\fR(7))
70 for details\&.
71 .RE
72 .PP
73 \fInew_table\fR
74 .RS 4
75 The name (optionally schema\-qualified) of the table to be created\&.
76 .RE
77 .PP
78 All other parameters are described in detail under
79 \fBSELECT\fR(7)\&.
80 .SH "NOTES"
81 .PP
82 \fBCREATE TABLE AS\fR
83 is functionally similar to
84 \fBSELECT INTO\fR\&.
85 \fBCREATE TABLE AS\fR
86 is the recommended syntax, since this form of
87 \fBSELECT INTO\fR
88 is not available in
89 ECPG
90 or
91 PL/pgSQL, because they interpret the
92 INTO
93 clause differently\&. Furthermore,
94 \fBCREATE TABLE AS\fR
95 offers a superset of the functionality provided by
96 \fBSELECT INTO\fR\&.
97 .PP
98 In contrast to
99 \fBCREATE TABLE AS\fR,
100 \fBSELECT INTO\fR
101 does not allow specifying properties like a table\*(Aqs access method with
102 USING \fImethod\fR
103 or the table\*(Aqs tablespace with
104 TABLESPACE \fItablespace_name\fR\&. Use
105 \fBCREATE TABLE AS\fR
106 if necessary\&. Therefore, the default table access method is chosen for the new table\&. See
107 default_table_access_method
108 for more information\&.
109 .SH "EXAMPLES"
110 .PP
111 Create a new table
112 films_recent
113 consisting of only recent entries from the table
114 films:
115 .sp
116 .if n \{\
117 .RS 4
118 .\}
119 .nf
120 SELECT * INTO films_recent FROM films WHERE date_prod >= \*(Aq2002\-01\-01\*(Aq;
121 .fi
122 .if n \{\
123 .RE
124 .\}
125 .SH "COMPATIBILITY"
126 .PP
127 The SQL standard uses
128 \fBSELECT INTO\fR
129 to represent selecting values into scalar variables of a host program, rather than creating a new table\&. This indeed is the usage found in
130 ECPG
131 (see
132 Chapter\ \&34) and
133 PL/pgSQL
134 (see
135 Chapter\ \&41)\&. The
136 PostgreSQL
137 usage of
138 \fBSELECT INTO\fR
139 to represent table creation is historical\&. Some other SQL implementations also use
140 \fBSELECT INTO\fR
141 in this way (but most SQL implementations support
142 \fBCREATE TABLE AS\fR
143 instead)\&. Apart from such compatibility considerations, it is best to use
144 \fBCREATE TABLE AS\fR
145 for this purpose in new code\&.
146 .SH "SEE ALSO"
147 CREATE TABLE AS (\fBCREATE_TABLE_AS\fR(7))