r"""Construct a :class:`_expression.Values` construct representing the SQL ``VALUES`` clause. The column expressions and the actual data for :class:`_expression.Values` are given in two separate steps. The constructor receives the column expressions typically as :func:`_expression.
(
*columns: _OnlyColumnArgument[Any],
name: Optional[str] = None,
literal_binds: bool = False,
)
| 752 | |
| 753 | |
| 754 | def values( |
| 755 | *columns: _OnlyColumnArgument[Any], |
| 756 | name: Optional[str] = None, |
| 757 | literal_binds: bool = False, |
| 758 | ) -> Values: |
| 759 | r"""Construct a :class:`_expression.Values` construct representing the |
| 760 | SQL ``VALUES`` clause. |
| 761 | |
| 762 | The column expressions and the actual data for :class:`_expression.Values` |
| 763 | are given in two separate steps. The constructor receives the column |
| 764 | expressions typically as :func:`_expression.column` constructs, and the |
| 765 | data is then passed via the :meth:`_expression.Values.data` method as a |
| 766 | list, which can be called multiple times to add more data, e.g.:: |
| 767 | |
| 768 | from sqlalchemy import column |
| 769 | from sqlalchemy import values |
| 770 | from sqlalchemy import Integer |
| 771 | from sqlalchemy import String |
| 772 | |
| 773 | value_expr = ( |
| 774 | values( |
| 775 | column("id", Integer), |
| 776 | column("name", String), |
| 777 | ) |
| 778 | .data([(1, "name1"), (2, "name2")]) |
| 779 | .data([(3, "name3")]) |
| 780 | ) |
| 781 | |
| 782 | Would represent a SQL fragment like:: |
| 783 | |
| 784 | VALUES(1, "name1"), (2, "name2"), (3, "name3") |
| 785 | |
| 786 | The :class:`_sql.values` construct has an optional |
| 787 | :paramref:`_sql.values.name` field; when using this field, the |
| 788 | PostgreSQL-specific "named VALUES" clause may be generated:: |
| 789 | |
| 790 | value_expr = values( |
| 791 | column("id", Integer), column("name", String), name="somename" |
| 792 | ).data([(1, "name1"), (2, "name2"), (3, "name3")]) |
| 793 | |
| 794 | When selecting from the above construct, the name and column names will |
| 795 | be listed out using a PostgreSQL-specific syntax:: |
| 796 | |
| 797 | >>> print(value_expr.select()) |
| 798 | SELECT somename.id, somename.name |
| 799 | FROM (VALUES (:param_1, :param_2), (:param_3, :param_4), |
| 800 | (:param_5, :param_6)) AS somename (id, name) |
| 801 | |
| 802 | For a more database-agnostic means of SELECTing named columns from a |
| 803 | VALUES expression, the :meth:`.Values.cte` method may be used, which |
| 804 | produces a named CTE with explicit column names against the VALUES |
| 805 | construct within; this syntax works on PostgreSQL, SQLite, and MariaDB:: |
| 806 | |
| 807 | value_expr = ( |
| 808 | values( |
| 809 | column("id", Integer), |
| 810 | column("name", String), |
| 811 | ) |