Available in versions: Dev (3.22)
This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.
Table valued functions
Supported by ❌ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
A table valued function is a function that returns a table, rather than a single scalar value. Such functions can be used in the FROM clause, and elsewhere, where table expressions can be used. In many ways, they work like parameterised views.
A table valued function declares the columns of the table it returns using the RETURNS TABLE clause, instead of a scalar data type. A simple example for creating such a function is:
// Create a function that returns all books by a given author
Parameter<Integer> authorId = in("author_id", INTEGER);
create.createFunction("books_by_author")
.parameters(authorId)
.returnsTable(
field(name("id"), INTEGER),
field(name("title"), VARCHAR(400))
)
.readsSQLData()
.as(select(BOOK.ID, BOOK.TITLE)
.from(BOOK)
.where(BOOK.AUTHOR_ID.eq(authorId)))
.execute();
Once you've created the above function, you can run code generation to get a type safe stub for calling it, just like any other table valued function:
// Call the previously created function with generated code: create.selectFrom(booksByAuthor(1)).fetch();
Yielding:
+----+-------------+ | id | title | +----+-------------+ | 1 | 1984 | | 2 | Animal Farm | +----+-------------+
The function body
The syntax for table valued functions varies a lot between RDBMS. If the function body consists of a single query, i.e. a SELECT statement, or a return_(select(..)) statement, optionally wrapped in a block containing only that statement, then jOOQ generates the dialect specific syntax for you. For example, the above function is generated as follows in PostgreSQL:
CREATE FUNCTION books_by_author (author_id int) RETURNS TABLE ( id int, title varchar ) LANGUAGE sql AS $$ SELECT book.id, book.title FROM book WHERE book.author_id = author_id $$
And as follows in SQL Server, where only inline table valued functions are supported, which don't have a column list. The declared column names are applied using a derived column list instead. Declared column types are not enforced:
CREATE FUNCTION books_by_author (@author_id int) RETURNS TABLE AS RETURN SELECT t.id, t.title FROM ( SELECT book.id, book.title FROM book WHERE book.author_id = @author_id ) t (id, title)
These are the dialect specific ways of returning a single query:
- BigQuery:
CREATE TABLE FUNCTION .. RETURNS TABLE<..> AS (<query>) - Db2, Databricks:
RETURN <query> - Firebird: an emulation using a selectable procedure,
CREATE PROCEDURE .. RETURNS (..) AS BEGIN FOR <query> INTO .. DO SUSPEND; END - HANA:
AS BEGIN RETURN <query>; END - HSQLDB and the SQL standard:
RETURN TABLE (<query>) - PostgreSQL, YugabyteDB:
LANGUAGE sql AS $$ <query> $$ - SQL Server, Azure Synapse:
AS RETURN <query>
Any other function body is generated as an ordinary procedural block, which then has to produce the function's result using vendor specific means, e.g. PostgreSQL's RETURN QUERY statement, which can be embedded using plain SQL:
create.createFunction("two_rows")
.returnsTable(field(name("x"), INTEGER))
.as(
statement("return query select 1"),
statement("return query select 2")
)
.execute();
The ON NULL INPUT characteristic
The RETURNS NULL ON NULL INPUT characteristic is supported also for table valued functions, in case of which the function returns an empty table if any argument value is NULL. If not natively supported by your dialect, jOOQ emulates this by filtering the query's result using a WHERE (arg1, arg2, ..) IS NOT NULL predicate.
Dialect support
Dialect support
This example using jOOQ:
createFunction("f").returnsTable(field(name("id"), INTEGER), field(name("title"), VARCHAR)).as(select(BOOK.ID, BOOK.TITLE).from(BOOK))
Translates to the following dialect specific expressions:
Aurora Postgres, Postgres, YugabyteDB
CREATE FUNCTION f() RETURNS TABLE ( id int, title varchar ) LANGUAGE SQL AS $$ SELECT BOOK.ID, BOOK.TITLE FROM BOOK $$
BigQuery
CREATE TABLE FUNCTION f()
RETURNS TABLE<id int64, title string>
AS (
SELECT t.id, t.title
FROM (
SELECT
BOOK.ID id,
BOOK.TITLE title
FROM BOOK
) t
)
DB2
CREATE FUNCTION f() RETURNS TABLE ( id integer, title varchar(32672) ) RETURN SELECT BOOK.ID, BOOK.TITLE FROM BOOK
Databricks
CREATE FUNCTION f() RETURNS TABLE ( id int, title string ) RETURN SELECT BOOK.ID, BOOK.TITLE FROM BOOK
Firebird
CREATE PROCEDURE f()
RETURNS (
id integer,
title varchar(4000)
)
AS
BEGIN
FOR
SELECT BOOK.ID, BOOK.TITLE
FROM BOOK
INTO :id, :title
DO
SUSPEND;
END
HSQLDB
CREATE FUNCTION f() RETURNS TABLE ( id int, title varchar(32672) ) RETURN TABLE ( SELECT BOOK.ID, BOOK.TITLE FROM BOOK )
Hana
CREATE FUNCTION f() RETURNS TABLE ( id integer, title varchar ) AS BEGIN RETURN SELECT BOOK.ID, BOOK.TITLE FROM BOOK; END
SQLDataWarehouse, SQLServer
CREATE FUNCTION f() RETURNS TABLE AS RETURN SELECT t.id, t.title FROM ( SELECT BOOK.ID, BOOK.TITLE FROM BOOK ) t (id, title)
ASE, Access, Aurora MySQL, ClickHouse, CockroachDB, DuckDB, Exasol, H2, Informix, MariaDB, MemSQL, MySQL, Oracle, Redshift, SQLite, Snowflake, Spanner, Sybase, Teradata, Trino, Vertica
/* UNSUPPORTED */
Generated with jOOQ 3.22. Support in older jOOQ versions may differ. Translate your own SQL on our website
Some of these dialects need special syntax to drop a table valued function again, e.g. DROP TABLE FUNCTION in BigQuery, or DROP PROCEDURE for Firebird's emulation. Use the DROP TABLE FUNCTION statement to drop table valued functions in a vendor agnostic way.
References to this page
Feedback
Do you have any feedback about this page? We'd love to hear it!