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.

Named TVF calls

Supported by ❌ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

When no generated table-valued function is available, e.g. because the function is created dynamically, or because you are not using the code generator, a table-valued function can still be called on the fly. A table-valued function call is very similar to a stored procedure call, which is why the API re-uses the CALL statement's DSL.call() method. Pass the function's (possibly qualified) Name and its arguments, and then use returnsTable() to turn the procedure call into a table-valued function call. Using the same F_BOOKS function as in the previous section:

SELECT *
FROM f_books(1)
create.fetch(call(name("f_books"))
    .args(val(1))
    .returnsTable());

The resulting org.jooq.CallTableFunction type is a org.jooq.ResultQuery, which is executed as a SELECT statement. It is also a org.jooq.TableLike, so the call can be embedded in the FROM clause of other queries, using TableLike.asTable(), e.g. to CROSS APPLY or LATERAL join it to other tables.

Without any arguments to returnsTable(), the call does not know its columns, i.e. Fields.fields() returns an empty array. The columns are discovered only when executing the query. If you want to dereference columns from the table-valued function, you can describe its row type by passing a list of named table columns:

SELECT b.id, b.title
FROM f_books(1) AS b
WHERE b.title LIKE '%SQL%'
// Describe the row type of the table-valued function
Table<?> b = call(name("f_books"))
    .args(val(1))
    .returnsTable(field(name("id"), INTEGER), field(name("title"), VARCHAR))
    .asTable("b");

// Dereference the columns from the table-valued function
Field<Integer> id = b.field("id", INTEGER);
Field<String> title = b.field("title", VARCHAR);

create.select(id, title)
      .from(b)
      .where(title.like("%SQL%"))
      .fetch();

The same works for scalar functions, using returns(). This produces a org.jooq.CallFunction, which is also a org.jooq.ResultQuery, and which can be embedded in other queries as a type safe Field<T> using CallFunction.asField():

-- Executed directly
SELECT my_sum(1, 2)

-- Embedded in another query
SELECT book.id, my_sum(book.id, 1)
FROM book
WHERE my_sum(book.id, 1) > 2
// Executed directly
Integer sum = create.fetchValue(call(name("my_sum"))
    .args(val(1), val(2))
    .returns(INTEGER));

// Embedded in another query, as a type safe Field<Integer>
Field<Integer> mySum = call(name("my_sum")).args(BOOK.ID, val(1)).returns(INTEGER).asField();

create.select(BOOK.ID, mySum)
      .from(BOOK)
      .where(mySum.gt(2))
      .fetch();

Just like with named table columns, these names are quoted by default, and thus case sensitive. For more details, please refer to the section about names and identifiers.

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo