1VALUES(7)                PostgreSQL 12.2 Documentation               VALUES(7)
2
3
4

NAME

6       VALUES - compute a set of rows
7

SYNOPSIS

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

DESCRIPTION

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).
25
26       Within larger commands, VALUES is syntactically allowed anywhere that
27       SELECT is. Because it is treated like a SELECT by the grammar, it is
28       possible to use the ORDER BY, LIMIT (or equivalently FETCH FIRST), and
29       OFFSET clauses with a VALUES command.
30

PARAMETERS

32       expression
33           A constant or expression to compute and insert at the indicated
34           place in the resulting table (set of rows). In a VALUES list
35           appearing at the top level of an INSERT, an expression can be
36           replaced by DEFAULT to indicate that the destination column's
37           default value should be inserted.  DEFAULT cannot be used when
38           VALUES appears in other contexts.
39
40       sort_expression
41           An expression or integer constant indicating how to sort the result
42           rows. This expression can refer to the columns of the VALUES result
43           as column1, column2, etc. For more details see ORDER BY Clause.
44
45       operator
46           A sorting operator. For details see ORDER BY Clause.
47
48       count
49           The maximum number of rows to return. For details see LIMIT Clause.
50
51       start
52           The number of rows to skip before starting to return rows. For
53           details see LIMIT Clause.
54

NOTES

56       VALUES lists with very large numbers of rows should be avoided, as you
57       might encounter out-of-memory failures or poor performance.  VALUES
58       appearing within INSERT is a special case (because the desired column
59       types are known from the INSERT's target table, and need not be
60       inferred by scanning the VALUES list), so it can handle larger lists
61       than are practical in other contexts.
62

EXAMPLES

64       A bare VALUES command:
65
66           VALUES (1, 'one'), (2, 'two'), (3, 'three');
67
68       This will return a table of two columns and three rows. It's
69       effectively equivalent to:
70
71           SELECT 1 AS column1, 'one' AS column2
72           UNION ALL
73           SELECT 2, 'two'
74           UNION ALL
75           SELECT 3, 'three';
76
77       More usually, VALUES is used within a larger SQL command. The most
78       common use is in INSERT:
79
80           INSERT INTO films (code, title, did, date_prod, kind)
81               VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
82
83       In the context of INSERT, entries of a VALUES list can be DEFAULT to
84       indicate that the column default should be used here instead of
85       specifying a value:
86
87           INSERT INTO films VALUES
88               ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
89               ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);
90
91       VALUES can also be used where a sub-SELECT might be written, for
92       example in a FROM clause:
93
94           SELECT f.*
95             FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
96             WHERE f.studio = t.studio AND f.kind = t.kind;
97
98           UPDATE employees SET salary = salary * v.increase
99             FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
100             WHERE employees.depno = v.depno AND employees.sales >= v.target;
101
102       Note that an AS clause is required when VALUES is used in a FROM
103       clause, just as is true for SELECT. It is not required that the AS
104       clause specify names for all the columns, but it's good practice to do
105       so. (The default column names for VALUES are column1, column2, etc in
106       PostgreSQL, but these names might be different in other database
107       systems.)
108
109       When VALUES is used in INSERT, the values are all automatically coerced
110       to the data type of the corresponding destination column. When it's
111       used in other contexts, it might be necessary to specify the correct
112       data type. If the entries are all quoted literal constants, coercing
113       the first is sufficient to determine the assumed type for all:
114
115           SELECT * FROM machines
116           WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));
117
118           Tip
119           For simple IN tests, it's better to rely on the list-of-scalars
120           form of IN than to write a VALUES query as shown above. The list of
121           scalars method requires less writing and is often more efficient.
122

COMPATIBILITY

124       VALUES conforms to the SQL standard.  LIMIT and OFFSET are PostgreSQL
125       extensions; see also under SELECT(7).
126

SEE ALSO

128       INSERT(7), SELECT(7)
129
130
131
132PostgreSQL 12.2                      2020                            VALUES(7)
Impressum