Updated on 2024-08-20 GMT+08:00

VALUES

Function

Computes a row or a set of rows based on given values. It is most commonly used to generate a constant table within a large statement.

Precautions

  • VALUES lists with large numbers of rows should be avoided, as you might encounter out-of-memory failures or poor performance. VALUES appearing within INSERT is a special case, because the desired column types are known from the INSERT's target table, and need not be inferred by scanning the VALUES list. In this case, a large number of result rows can be processed.
  • If more than one row is specified, all the rows must have the same number of elements.

Syntax

1
2
3
4
5
VALUES {( expression [, ...] )} [, ...]
    [ ORDER BY { sort_expression [ ASC | DESC | USING operator ] } [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ];

Parameter Description

  • expression

    Specifies a constant or expression to compute and insert at the indicated place in the resulting table or set of rows.

    In a VALUES list appearing at the top level of an INSERT, an expression can be replaced by DEFAULT to indicate that the destination column's default value should be inserted. DEFAULT cannot be used when VALUES appears in other contexts.

  • sort_expression

    Specifies an expression or integer constant indicating how to sort the result rows.

  • ASC

    Specifies an ascending sort order.

  • DESC

    Specifies a descending sort order.

  • operator

    Specifies a sorting operator.

  • count

    Specifies the maximum number of rows to return.

  • OFFSET start [ ROW | ROWS ]

    Specifies the maximum number of returned rows, whereas start specifies the number of rows to skip before starting to return rows.

  • FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY

    The FETCH clause restricts the total number of rows starting from the first row of the return query result, and the default value of count is 1.

Examples

See Examples in INSERT.