插入不存在的值 - ORACLE/Postgres
Inserting values if they don't already exist - ORACLE/Postgres
我在 liquibase 中有回滚,例如:
<changeSet author="..." id="...">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">
...
</sqlCheck>
</preConditions>
<delete tableName="A">
<where>ID = 'house'</where>
</delete>
<rollback>
<insert tableName="A">
<column name="ID" value="house" />
<column name="TITLE" value="a" />
<column name="TYPE" value="b" />
<column name="VER" valueNumeric="c" />
</insert>
</rollback>
</changeSet>
并且此变更集 运行 在 3 个不同的 xml 文件中,每个文件都相同。
我需要提供自定义 sql 查询来回滚,因为它可能会抛出关于向数据库插入相同值的异常,所以我需要知道具有给定值的 ID 是否存在。
我做了这样的东西:
<rollback>
<sql dbms="oracle">
insert into A(ID,TITLE,TYPE,VER)
VALUES(house,a,b,c)
where not exists (select ID from A where ID = 'house')
</sql>
<sql dbms="postgresql">
...
</sql>
</rollback>
我做了这样的东西,但是没有用。 (对于 postgres,我也需要这样做)我该怎么做?我只需要检查 'A' table.
中是否存在具有给定值的 ID
INSERT INTO A(ID,TITLE,TYPE,VER)
VALUES(house,a,b,c)
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house')
应该适用于 postgres
使用 SELECT
作为来源,而不是 values
子句:
对于 Postgres:
INSERT INTO A(ID,TITLE,TYPE,VER)
select 'house','a','b','c'
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house');
对于甲骨文:
INSERT INTO A(ID,TITLE,TYPE,VER)
select 'house','a','b','c'
from dual
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house');
我在 liquibase 中有回滚,例如:
<changeSet author="..." id="...">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">
...
</sqlCheck>
</preConditions>
<delete tableName="A">
<where>ID = 'house'</where>
</delete>
<rollback>
<insert tableName="A">
<column name="ID" value="house" />
<column name="TITLE" value="a" />
<column name="TYPE" value="b" />
<column name="VER" valueNumeric="c" />
</insert>
</rollback>
</changeSet>
并且此变更集 运行 在 3 个不同的 xml 文件中,每个文件都相同。
我需要提供自定义 sql 查询来回滚,因为它可能会抛出关于向数据库插入相同值的异常,所以我需要知道具有给定值的 ID 是否存在。
我做了这样的东西:
<rollback>
<sql dbms="oracle">
insert into A(ID,TITLE,TYPE,VER)
VALUES(house,a,b,c)
where not exists (select ID from A where ID = 'house')
</sql>
<sql dbms="postgresql">
...
</sql>
</rollback>
我做了这样的东西,但是没有用。 (对于 postgres,我也需要这样做)我该怎么做?我只需要检查 'A' table.
中是否存在具有给定值的 IDINSERT INTO A(ID,TITLE,TYPE,VER)
VALUES(house,a,b,c)
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house')
应该适用于 postgres
使用 SELECT
作为来源,而不是 values
子句:
对于 Postgres:
INSERT INTO A(ID,TITLE,TYPE,VER)
select 'house','a','b','c'
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house');
对于甲骨文:
INSERT INTO A(ID,TITLE,TYPE,VER)
select 'house','a','b','c'
from dual
WHERE NOT EXISTS (SELECT 1 from A where ID = 'house');