1SELECT INTO(7) PostgreSQL 9.2.24 Documentation SELECT INTO(7)
2
3
4
6 SELECT_INTO - define a new table from the results of a query
7
9 [ WITH [ RECURSIVE ] with_query [, ...] ]
10 SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
11 * | expression [ [ AS ] output_name ] [, ...]
12 INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table
13 [ FROM from_item [, ...] ]
14 [ WHERE condition ]
15 [ GROUP BY expression [, ...] ]
16 [ HAVING condition [, ...] ]
17 [ WINDOW window_name AS ( window_definition ) [, ...] ]
18 [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
19 [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
20 [ LIMIT { count | ALL } ]
21 [ OFFSET start [ ROW | ROWS ] ]
22 [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
23 [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]
24
26 SELECT INTO creates a new table and fills it with data computed by a
27 query. The data is not returned to the client, as it is with a normal
28 SELECT. The new table's columns have the names and data types
29 associated with the output columns of the SELECT.
30
32 TEMPORARY or TEMP
33 If specified, the table is created as a temporary table. Refer to
34 CREATE TABLE (CREATE_TABLE(7)) for details.
35
36 UNLOGGED
37 If specified, the table is created as an unlogged table. Refer to
38 CREATE TABLE (CREATE_TABLE(7)) for details.
39
40 new_table
41 The name (optionally schema-qualified) of the table to be created.
42
43 All other parameters are described in detail under SELECT(7).
44
46 CREATE TABLE AS (CREATE_TABLE_AS(7)) is functionally similar to SELECT
47 INTO. CREATE TABLE AS is the recommended syntax, since this form of
48 SELECT INTO is not available in ECPG or PL/pgSQL, because they
49 interpret the INTO clause differently. Furthermore, CREATE TABLE AS
50 offers a superset of the functionality provided by SELECT INTO.
51
52 Prior to PostgreSQL 8.1, the table created by SELECT INTO included OIDs
53 by default. In PostgreSQL 8.1, this is not the case — to include OIDs
54 in the new table, the default_with_oids configuration variable must be
55 enabled. Alternatively, CREATE TABLE AS can be used with the WITH OIDS
56 clause.
57
59 Create a new table films_recent consisting of only recent entries from
60 the table films:
61
62 SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';
63
65 The SQL standard uses SELECT INTO to represent selecting values into
66 scalar variables of a host program, rather than creating a new table.
67 This indeed is the usage found in ECPG (see Chapter 33, ECPG - Embedded
68 SQL in C, in the documentation) and PL/pgSQL (see Chapter 39, PL/pgSQL
69 - SQL Procedural Language, in the documentation). The PostgreSQL usage
70 of SELECT INTO to represent table creation is historical. It is best to
71 use CREATE TABLE AS for this purpose in new code.
72
74 CREATE TABLE AS (CREATE_TABLE_AS(7))
75
76
77
78PostgreSQL 9.2.24 2017-11-06 SELECT INTO(7)