如果不存在,HSQLDB 将列添加到 table
HSQLDB add column to table if not exists
尝试将列 column1
添加到 table1
(如果它尚不存在):
CREATE PROCEDURE column_present()
MODIFIES SQL DATA
BEGIN ATOMIC
DECLARE column_count integer;
set column_count = select COUNT(*) from information_schema.system_columns Where table_name = 'table1' and column_name = 'column1';
if column_count = 0 then alter table table1 ADD column1 integer; end if;
END;
结果:
[2019-05-03 22:28:13] [42581][-5581] unexpected token: ALTER : line: 6
[2019-05-03 22:28:13] java.lang.RuntimeException: org.hsqldb.HsqlException: unexpected token: ALTER : line: 6
[2019-05-03 22:28:13] at org.hsqldb.error.Error.parseError(Unknown Source)
在 HSQLDB 中现有 table(如果它尚不存在)中创建列的正确方法是什么?
请注意:忽略创建错误(如果它已经存在)对我来说不是一个选项。
在 hsqldb 邮件列表 (https://sourceforge.net/p/hsqldb/mailman/message/36657341/) 上得到了答案:
Although we can add IF NOT EXISTS to most database object creation
statements, this is not possible for ALTER statements.
It is also not possible to use DDL statements inside a PROCEDURE.
We may add one of these capabilities to the next version, 2.5.0, in the
coming days.
所以目前显然是不可能的
尝试将列 column1
添加到 table1
(如果它尚不存在):
CREATE PROCEDURE column_present()
MODIFIES SQL DATA
BEGIN ATOMIC
DECLARE column_count integer;
set column_count = select COUNT(*) from information_schema.system_columns Where table_name = 'table1' and column_name = 'column1';
if column_count = 0 then alter table table1 ADD column1 integer; end if;
END;
结果:
[2019-05-03 22:28:13] [42581][-5581] unexpected token: ALTER : line: 6
[2019-05-03 22:28:13] java.lang.RuntimeException: org.hsqldb.HsqlException: unexpected token: ALTER : line: 6
[2019-05-03 22:28:13] at org.hsqldb.error.Error.parseError(Unknown Source)
在 HSQLDB 中现有 table(如果它尚不存在)中创建列的正确方法是什么?
请注意:忽略创建错误(如果它已经存在)对我来说不是一个选项。
在 hsqldb 邮件列表 (https://sourceforge.net/p/hsqldb/mailman/message/36657341/) 上得到了答案:
Although we can add IF NOT EXISTS to most database object creation statements, this is not possible for ALTER statements.
It is also not possible to use DDL statements inside a PROCEDURE.
We may add one of these capabilities to the next version, 2.5.0, in the coming days.
所以目前显然是不可能的