Available in versions: Dev (3.18) | Latest (3.17) | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10 | 3.9 | 3.8

From a SELECT

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

Occasionally, creating a table from a SELECT statement is very useful, copying the source table's data types and data.

// Create a new table from a source SELECT statement
create.createTable("book_archive")
      .as(select(BOOK.ID, BOOK.TITLE).from(BOOK))
      .execute();

Dialect support

This example using jOOQ:

createTable("book_archive")
      .as(select(BOOK.ID, BOOK.TITLE).from(BOOK))

Translates to the following dialect specific expressions:

-- ACCESS, ASE, SQLDATAWAREHOUSE, SQLSERVER
SELECT BOOK.ID, BOOK.TITLE
INTO book_archive
FROM BOOK

-- AURORA_MYSQL, AURORA_POSTGRES, COCKROACHDB, DERBY, EXASOL, H2, MARIADB, MEMSQL, MYSQL, ORACLE, POSTGRES, REDSHIFT, 
-- SNOWFLAKE, SQLITE, VERTICA, YUGABYTEDB
CREATE TABLE book_archive
AS
SELECT BOOK.ID, BOOK.TITLE
FROM BOOK

-- DB2
BEGIN
  EXECUTE IMMEDIATE '
    CREATE TABLE book_archive
    AS (
      SELECT BOOK.ID, BOOK.TITLE
      FROM BOOK
    ) WITH NO DATA
  ';
  EXECUTE IMMEDIATE '
    INSERT INTO book_archive
    SELECT BOOK.ID, BOOK.TITLE
    FROM BOOK
  ';
END

-- HANA
CREATE TABLE book_archive
AS (
  SELECT BOOK.ID, BOOK.TITLE
  FROM BOOK
)

-- HSQLDB, TERADATA
CREATE TABLE book_archive
AS (
  SELECT BOOK.ID, BOOK.TITLE
  FROM BOOK
)
WITH DATA

-- BIGQUERY, FIREBIRD, INFORMIX, SYBASE
/* UNSUPPORTED */

(These are currently generated with jOOQ 3.18, 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