MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / cte

Method cte

lib/sqlalchemy/sql/selectable.py:2714–3009  ·  view source on GitHub ↗

r"""Return a new :class:`_expression.CTE`, or Common Table Expression instance. Common table expressions are a SQL standard whereby SELECT statements can draw upon secondary statements specified along with the primary statement, using a clause called "WITH".

(
        self,
        name: Optional[str] = None,
        recursive: bool = False,
        nesting: bool = False,
    )

Source from the content-addressed store, hash-verified

2712 return self
2713
2714 def cte(
2715 self,
2716 name: Optional[str] = None,
2717 recursive: bool = False,
2718 nesting: bool = False,
2719 ) -> CTE:
2720 r"""Return a new :class:`_expression.CTE`,
2721 or Common Table Expression instance.
2722
2723 Common table expressions are a SQL standard whereby SELECT
2724 statements can draw upon secondary statements specified along
2725 with the primary statement, using a clause called "WITH".
2726 Special semantics regarding UNION can also be employed to
2727 allow "recursive" queries, where a SELECT statement can draw
2728 upon the set of rows that have previously been selected.
2729
2730 CTEs can also be applied to DML constructs UPDATE, INSERT
2731 and DELETE on some databases, both as a source of CTE rows
2732 when combined with RETURNING, as well as a consumer of
2733 CTE rows.
2734
2735 SQLAlchemy detects :class:`_expression.CTE` objects, which are treated
2736 similarly to :class:`_expression.Alias` objects, as special elements
2737 to be delivered to the FROM clause of the statement as well
2738 as to a WITH clause at the top of the statement.
2739
2740 For special prefixes such as PostgreSQL "MATERIALIZED" and
2741 "NOT MATERIALIZED", the :meth:`_expression.CTE.prefix_with`
2742 method may be
2743 used to establish these.
2744
2745 :param name: name given to the common table expression. Like
2746 :meth:`_expression.FromClause.alias`, the name can be left as
2747 ``None`` in which case an anonymous symbol will be used at query
2748 compile time.
2749 :param recursive: if ``True``, will render ``WITH RECURSIVE``.
2750 A recursive common table expression is intended to be used in
2751 conjunction with UNION ALL in order to derive rows
2752 from those already selected.
2753 :param nesting: if ``True``, will render the CTE locally to the
2754 statement in which it is referenced. For more complex scenarios,
2755 the :meth:`.HasCTE.add_cte` method using the
2756 :paramref:`.HasCTE.add_cte.nest_here`
2757 parameter may also be used to more carefully
2758 control the exact placement of a particular CTE.
2759
2760 .. versionadded:: 1.4.24
2761
2762 .. seealso::
2763
2764 :meth:`.HasCTE.add_cte`
2765
2766 The following examples include two from PostgreSQL's documentation at
2767 https://www.postgresql.org/docs/current/static/queries-with.html,
2768 as well as additional examples.
2769
2770 Example 1, non recursive::
2771

Callers 2

_factoryMethod · 0.45
cteFunction · 0.45

Calls 1

_constructMethod · 0.45

Tested by

no test coverage detected