1VALUES(7) PostgreSQL 9.2.24 Documentation VALUES(7)
2
3
4
6 VALUES - compute a set of rows
7
9 VALUES ( expression [, ...] ) [, ...]
10 [ ORDER BY sort_expression [ ASC | DESC | USING operator ] [, ...] ]
11 [ LIMIT { count | ALL } ]
12 [ OFFSET start [ ROW | ROWS ] ]
13 [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
14
16 VALUES computes a row value or set of row values specified by value
17 expressions. It is most commonly used to generate a “constant table”
18 within a larger command, but it can be used on its own.
19
20 When more than one row is specified, all the rows must have the same
21 number of elements. The data types of the resulting table's columns are
22 determined by combining the explicit or inferred types of the
23 expressions appearing in that column, using the same rules as for UNION
24 (see Section 10.5, “UNION, CASE, and Related Constructs”, in the
25 documentation).
26
27 Within larger commands, VALUES is syntactically allowed anywhere that
28 SELECT is. Because it is treated like a SELECT by the grammar, it is
29 possible to use the ORDER BY, LIMIT (or equivalently FETCH FIRST), and
30 OFFSET clauses with a VALUES command.
31
33 expression
34 A constant or expression to compute and insert at the indicated
35 place in the resulting table (set of rows). In a VALUES list
36 appearing at the top level of an INSERT, an expression can be
37 replaced by DEFAULT to indicate that the destination column's
38 default value should be inserted. DEFAULT cannot be used when
39 VALUES appears in other contexts.
40
41 sort_expression
42 An expression or integer constant indicating how to sort the result
43 rows. This expression can refer to the columns of the VALUES result
44 as column1, column2, etc. For more details see ORDER BY Clause.
45
46 operator
47 A sorting operator. For details see ORDER BY Clause.
48
49 count
50 The maximum number of rows to return. For details see LIMIT Clause.
51
52 start
53 The number of rows to skip before starting to return rows. For
54 details see LIMIT Clause.
55
57 VALUES lists with very large numbers of rows should be avoided, as you
58 might encounter out-of-memory failures or poor performance. VALUES
59 appearing within INSERT is a special case (because the desired column
60 types are known from the INSERT's target table, and need not be
61 inferred by scanning the VALUES list), so it can handle larger lists
62 than are practical in other contexts.
63
65 A bare VALUES command:
66
67 VALUES (1, 'one'), (2, 'two'), (3, 'three');
68
69 This will return a table of two columns and three rows. It's
70 effectively equivalent to:
71
72 SELECT 1 AS column1, 'one' AS column2
73 UNION ALL
74 SELECT 2, 'two'
75 UNION ALL
76 SELECT 3, 'three';
77
78 More usually, VALUES is used within a larger SQL command. The most
79 common use is in INSERT:
80
81 INSERT INTO films (code, title, did, date_prod, kind)
82 VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
83
84 In the context of INSERT, entries of a VALUES list can be DEFAULT to
85 indicate that the column default should be used here instead of
86 specifying a value:
87
88 INSERT INTO films VALUES
89 ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
90 ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);
91
92 VALUES can also be used where a sub-SELECT might be written, for
93 example in a FROM clause:
94
95 SELECT f.*
96 FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
97 WHERE f.studio = t.studio AND f.kind = t.kind;
98
99 UPDATE employees SET salary = salary * v.increase
100 FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
101 WHERE employees.depno = v.depno AND employees.sales >= v.target;
102
103 Note that an AS clause is required when VALUES is used in a FROM
104 clause, just as is true for SELECT. It is not required that the AS
105 clause specify names for all the columns, but it's good practice to do
106 so. (The default column names for VALUES are column1, column2, etc in
107 PostgreSQL, but these names might be different in other database
108 systems.)
109
110 When VALUES is used in INSERT, the values are all automatically coerced
111 to the data type of the corresponding destination column. When it's
112 used in other contexts, it might be necessary to specify the correct
113 data type. If the entries are all quoted literal constants, coercing
114 the first is sufficient to determine the assumed type for all:
115
116 SELECT * FROM machines
117 WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));
118
119 Tip
120 For simple IN tests, it's better to rely on the list-of-scalars
121 form of IN than to write a VALUES query as shown above. The list of
122 scalars method requires less writing and is often more efficient.
123
125 VALUES conforms to the SQL standard. LIMIT and OFFSET are PostgreSQL
126 extensions; see also under SELECT(7).
127
129 INSERT(7), SELECT(7)
130
131
132
133PostgreSQL 9.2.24 2017-11-06 VALUES(7)