Available in versions: Dev (3.21) | Latest (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11
ALTER TABLE .. ADD COLUMN IF NOT EXISTS
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
A popular subclause of DDL statements that jOOQ can usually emulate, is the IF NOT EXISTS clause:
Dialect support
This example using jOOQ:
alterTable("t").addIfNotExists("c", INTEGER)
Translates to the following dialect specific expressions:
Aurora Postgres, DuckDB, Exasol, H2, MariaDB, Postgres, YugabyteDB
ALTER TABLE t ADD IF NOT EXISTS c int
BigQuery
ALTER TABLE t ADD COLUMN IF NOT EXISTS c int64
ClickHouse
ALTER TABLE t ADD COLUMN IF NOT EXISTS c Nullable(integer)
CockroachDB
ALTER TABLE t ADD IF NOT EXISTS c int4
Databricks
ALTER TABLE t ADD COLUMN IF NOT EXISTS (c int)
DB2
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '42711' BEGIN END;
EXECUTE IMMEDIATE '
ALTER TABLE t ADD c integer
';
END
Firebird
EXECUTE BLOCK
AS
BEGIN
EXECUTE STATEMENT '
ALTER TABLE t ADD c integer
';
WHEN sqlcode -607 DO
BEGIN END
END
Hana
DO BEGIN
DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 308 BEGIN END;
EXECUTE IMMEDIATE '
ALTER TABLE t ADD (c integer)
';
END;
Oracle
BEGIN
EXECUTE IMMEDIATE '
ALTER TABLE t ADD c number(10)
';
EXCEPTION
WHEN others THEN
IF sqlerrm LIKE 'ORA-01430%' THEN NULL;
ELSE RAISE;
END IF;
END;
Spanner
ALTER TABLE t ADD IF NOT EXISTS c int64
SQLDataWarehouse
BEGIN TRY
EXEC sp_executesql N'
ALTER TABLE t ADD c int
';
END TRY
BEGIN CATCH
IF error_number() NOT IN (2705) BEGIN
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
END;
END CATCH
SQLServer
BEGIN TRY
EXEC sp_executesql N'
ALTER TABLE t ADD c int
';
END TRY
BEGIN CATCH
IF error_number() NOT IN (2705) THROW;
END CATCH
Teradata
ALTER TABLE t ADD IF NOT EXISTS c integer
Trino
ALTER TABLE t ADD COLUMN IF NOT EXISTS c int
ASE, Access, Aurora MySQL, HSQLDB, Informix, MemSQL, MySQL, Redshift, SQLite, Snowflake, Sybase, Vertica
/* UNSUPPORTED */
Generated with jOOQ 3.21. Support in older jOOQ versions may differ. Translate your own SQL on our website
Feedback
Do you have any feedback about this page? We'd love to hear it!