MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / table_valued

Method table_valued

lib/sqlalchemy/sql/functions.py:238–342  ·  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

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