Available in versions: Dev (3.19) | Latest (3.18) | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12

EXECUTE statement

Applies to ❌ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

Many dialects support some way of running dynamic SQL from procedural code. For this, the EXECUTE or EXECUTE IMMEDIATE statements can be used.

In some dialects (e.g. Oracle PL/SQL), using EXECUTE is the only way to run DDL from procedural code.

For example:

-- PL/SQL
BEGIN
  EXECUTE IMMEDIATE 'CREATE TABLE t (col int)';
END;
// All dialects
create.begin(
  execute(createTable("t").column("col", INTEGER).getSQL())
).excute();

You could obviously just pass an arbitrary string to the EXECUTE statement, as in PL/SQL, but the above example shows how to use this approach also with dynamically created jOOQ statements, by calling Query.getSQL().

Dialect support

This example using jOOQ:

execute("create table t (i int)")

Translates to the following dialect specific expressions:

-- BIGQUERY, DB2, HANA, MARIADB, ORACLE
EXECUTE IMMEDIATE 'create table t (i int)'

-- FIREBIRD
EXECUTE STATEMENT 'create table t (i int)'

-- MYSQL

CREATE PROCEDURE block_1696006144789_8026069()
MODIFIES SQL DATA
BEGIN
  PREPARE s FROM 'create table t (i int)';
  EXECUTE s;
  DEALLOCATE PREPARE s;
END;
CALL block_1696006144789_8026069();
DROP PROCEDURE block_1696006144789_8026069;

-- POSTGRES, YUGABYTEDB
EXECUTE 'create table t (i int)'

-- SQLSERVER
EXECUTE ('create table t (i int)')

-- ACCESS, ASE, AURORA_MYSQL, AURORA_POSTGRES, COCKROACHDB, DERBY, DUCKDB, EXASOL, H2, HSQLDB, INFORMIX, MEMSQL, REDSHIFT, 
-- SNOWFLAKE, SQLDATAWAREHOUSE, SQLITE, SYBASE, TERADATA, TRINO, VERTICA
/* UNSUPPORTED */

(These are currently generated with jOOQ 3.19, see #10141), or translate your own on our website

Feedback

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

The jOOQ Logo