Search code examples
h2alter

H2 Add column only if column not exists


How to add a column to H2 database only if that column does not exists already in the table?

I am using schema.sql to create a database structure.

I have tried several combinations of code like this but nothing works:

ALTER TABLE IF NOT EXISTS TABLE_NAME.column_name ADD COLUMN column_name VARCHAR (50);

h2 version: 1.4.199


Solution

  • The valid syntax is

    ALTER TABLE TABLE_NAME ADD COLUMN IF NOT EXISTS COLUMN_NAME VARCHAR(50);
    

    https://h2database.com/html/commands.html#alter_table_add