MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / table_valued

Method table_valued

lib/sqlalchemy/sql/functions.py:247–351  ·  view source on GitHub ↗

r"""Return a :class:`_sql.TableValuedAlias` representation of this :class:`_functions.FunctionElement` with table-valued expressions added. e.g. to use the SQLite form of ``generate_series()`` (including hidden columns "start", "stop", "step"): .. sourcecode:: pycon

(
        self, *expr: _ColumnExpressionOrStrLabelArgument[Any], **kw: Any
    )

Source from the content-addressed store, hash-verified

245 return ScalarFunctionColumn(self, name, type_)
246
247 def table_valued(
248 self, *expr: _ColumnExpressionOrStrLabelArgument[Any], **kw: Any
249 ) -> TableValuedAlias:
250 r"""Return a :class:`_sql.TableValuedAlias` representation of this
251 :class:`_functions.FunctionElement` with table-valued expressions added.
252
253 e.g. to use the SQLite form of ``generate_series()`` (including
254 hidden columns "start", "stop", "step"):
255
256 .. sourcecode:: pycon+sql
257
258 >>> fn = func.generate_series(1, 5).table_valued(
259 ... "value", "start", "stop", "step"
260 ... )
261
262 >>> print(select(fn))
263 {printsql}SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step
264 FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1{stop}
265
266 >>> print(select(fn.c.value, fn.c.stop).where(fn.c.value > 2))
267 {printsql}SELECT anon_1.value, anon_1.stop
268 FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1
269 WHERE anon_1.value > :value_1{stop}
270
271 Backends like PostgreSQL need the accessed columns to be explicitly
272 named in "AS" clause. To achieve this, use
273 :meth:`_sql.TableValuedAlias.render_derived`; be sure to consult the
274 :ref:`PostgreSQL-specific documentation for table valued functions
275 <postgresql_table_valued>` for additional examples:
276
277 .. sourcecode:: pycon+sql
278
279 >>> fn = func.generate_series(1, 5).table_valued("value").render_derived()
280
281 >>> print(select(fn))
282 {printsql}SELECT anon_1.value FROM
283 generate_series(:generate_series_1, :generate_series_2) AS anon_1(value){stop}
284
285 A WITH ORDINALITY expression may be generated by passing the keyword
286 argument :paramref:`.FunctionElement.table_valued.with_ordinality`,
287 illustrated below using PostgreSQL&#x27;s syntax:
288
289 .. sourcecode:: pycon+sql
290
291 >>> fn = func.generate_series(4, 1, -1).table_valued(
292 ... "gen", with_ordinality="ordinality"
293 ... )
294 >>> print(select(fn.render_derived()))
295 {printsql}SELECT anon_1.gen, anon_1.ordinality
296 FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3)
297 WITH ORDINALITY AS anon_1(gen, ordinality)
298
299 :param \*expr: A series of string column names that will be added to the
300 ``.c`` collection of the resulting :class:`_sql.TableValuedAlias`
301 construct as columns. :func:`_sql.column` objects with or without
302 datatypes may also be used.
303
304 :param name: optional name to assign to the alias name that&#x27;s generated.

Calls 4

TableValueTypeClass · 0.85
_generateMethod · 0.45
popMethod · 0.45
aliasMethod · 0.45