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,
)
| 675 | |
| 676 | |
| 677 | def values( |
| 678 | *columns: _OnlyColumnArgument[Any], |
| 679 | name: Optional[str] = None, |
| 680 | literal_binds: bool = False, |
| 681 | ) -> Values: |
| 682 | r"""Construct a :class:`_expression.Values` construct representing the |
| 683 | SQL ``VALUES`` clause. |
| 684 | |
| 685 | The column expressions and the actual data for :class:`_expression.Values` |
| 686 | are given in two separate steps. The constructor receives the column |
| 687 | expressions typically as :func:`_expression.column` constructs, and the |
| 688 | data is then passed via the :meth:`_expression.Values.data` method as a |
| 689 | list, which can be called multiple times to add more data, e.g.:: |
| 690 | |
| 691 | from sqlalchemy import column |
| 692 | from sqlalchemy import values |
| 693 | from sqlalchemy import Integer |
| 694 | from sqlalchemy import String |
| 695 | |
| 696 | value_expr = ( |
| 697 | values( |
| 698 | column("id", Integer), |
| 699 | column("name", String), |
| 700 | ) |
| 701 | .data([(1, "name1"), (2, "name2")]) |
| 702 | .data([(3, "name3")]) |
| 703 | ) |
| 704 | |
| 705 | Would represent a SQL fragment like:: |
| 706 | |
| 707 | VALUES(1, "name1"), (2, "name2"), (3, "name3") |
| 708 | |
| 709 | The :class:`_sql.values` construct has an optional |
| 710 | :paramref:`_sql.values.name` field; when using this field, the |
| 711 | PostgreSQL-specific "named VALUES" clause may be generated:: |
| 712 | |
| 713 | value_expr = values( |
| 714 | column("id", Integer), column("name", String), name="somename" |
| 715 | ).data([(1, "name1"), (2, "name2"), (3, "name3")]) |
| 716 | |
| 717 | When selecting from the above construct, the name and column names will |
| 718 | be listed out using a PostgreSQL-specific syntax:: |
| 719 | |
| 720 | >>> print(value_expr.select()) |
| 721 | SELECT somename.id, somename.name |
| 722 | FROM (VALUES (:param_1, :param_2), (:param_3, :param_4), |
| 723 | (:param_5, :param_6)) AS somename (id, name) |
| 724 | |
| 725 | For a more database-agnostic means of SELECTing named columns from a |
| 726 | VALUES expression, the :meth:`.Values.cte` method may be used, which |
| 727 | produces a named CTE with explicit column names against the VALUES |
| 728 | construct within; this syntax works on PostgreSQL, SQLite, and MariaDB:: |
| 729 | |
| 730 | value_expr = ( |
| 731 | values( |
| 732 | column("id", Integer), |
| 733 | column("name", String), |
| 734 | ) |