为 db2 合并或插入忽略
merge into or insert ignore for db2
我觉得这应该相当简单,但我在找出执行此操作的最佳方法时遇到了问题
我在此 subitem_to_item_status table 中有许多子项具有状态,但我需要为所有尚不存在的子项插入记录。我不需要更新那里的内容,我真的只是想复制一个 'insert/ignore'
子项的示例数据
ITEM_SUBITEMT
item_subitem_id | creator_identifier
-----------------------------------------
12 12345
13 12345
14 12345
15 12345
16 12345
17 12345
18 12345
19 12345
20 12345
21 12345
22 12345
SUBITEM_TO_ITEM_STATUS
SUBITEM_ID | ITEM_STATUS_ID | CREATED_BY_IDENTIFIER
------------------------------------------------------------
12 1 12345
15 1 12345
16 1 12345
20 1 12345
所以对于这些数据,我只需要从第一个 table 插入没有 ID 12、15、16 或 20
的记录
MERGE INTO schema.SUBITEM_TO_ITEM_STATUS (SUBITEM_ID,ITEM_STATUS_ID, CREATED_BY_IDENTIFIER,ROWCHANGE,CREATED_AT) AS T
USING( (SELECT ITEM_SUBITEM_ID, 1, CREATOR_IDENTIFIER, NOW(),NOW() FROM schema.ITEM_SUBITEMT) AS S
ON S.ITEM_SUBITEM_ID = T.SUBITEM_ID
WHEN NOT MATCHED THEN INSERT;
为什么不使用 insert
?
insert into SUBITEM_TO_ITEM_STATUS (SUBITEM_ID, ITEM_STATUS_ID, CREATED_BY_IDENTIFIER)
select item_subitem_id, 1, creator_identifier
from ITEM_SUBITEMT
where item_subitem_id not in (12, 15, 16, 20);
如果您的意思是不想在 table 中出现重复项,您可以尝试:
insert into SUBITEM_TO_ITEM_STATUS (SUBITEM_ID, ITEM_STATUS_ID, CREATED_BY_IDENTIFIER)
select item_subitem_id, 1, creator_identifier
from ITEM_SUBITEMT s
where not exists (select 1
from SUBITEM_TO_ITEM_STATUS tis
where tis.item_subitem_id = s.item_subitem_id
);
错误的 MERGE
语句。
试试这个:
MERGE INTO schema.SUBITEM_TO_ITEM_STATUS T
USING (SELECT ITEM_SUBITEM_ID, 1 AS ITEM_STATUS_ID, creator_identifier FROM schema.ITEM_SUBITEMT) S ON S.ITEM_SUBITEM_ID = T.SUBITEM_ID
WHEN NOT MATCHED THEN
INSERT (SUBITEM_ID, ITEM_STATUS_ID, CREATED_BY_IDENTIFIER)
VALUES (S.item_subitem_id, S.ITEM_STATUS_ID, S.creator_identifier);
我觉得这应该相当简单,但我在找出执行此操作的最佳方法时遇到了问题
我在此 subitem_to_item_status table 中有许多子项具有状态,但我需要为所有尚不存在的子项插入记录。我不需要更新那里的内容,我真的只是想复制一个 'insert/ignore'
子项的示例数据
ITEM_SUBITEMT
item_subitem_id | creator_identifier
-----------------------------------------
12 12345
13 12345
14 12345
15 12345
16 12345
17 12345
18 12345
19 12345
20 12345
21 12345
22 12345
SUBITEM_TO_ITEM_STATUS
SUBITEM_ID | ITEM_STATUS_ID | CREATED_BY_IDENTIFIER
------------------------------------------------------------
12 1 12345
15 1 12345
16 1 12345
20 1 12345
所以对于这些数据,我只需要从第一个 table 插入没有 ID 12、15、16 或 20
的记录 MERGE INTO schema.SUBITEM_TO_ITEM_STATUS (SUBITEM_ID,ITEM_STATUS_ID, CREATED_BY_IDENTIFIER,ROWCHANGE,CREATED_AT) AS T
USING( (SELECT ITEM_SUBITEM_ID, 1, CREATOR_IDENTIFIER, NOW(),NOW() FROM schema.ITEM_SUBITEMT) AS S
ON S.ITEM_SUBITEM_ID = T.SUBITEM_ID
WHEN NOT MATCHED THEN INSERT;
为什么不使用 insert
?
insert into SUBITEM_TO_ITEM_STATUS (SUBITEM_ID, ITEM_STATUS_ID, CREATED_BY_IDENTIFIER)
select item_subitem_id, 1, creator_identifier
from ITEM_SUBITEMT
where item_subitem_id not in (12, 15, 16, 20);
如果您的意思是不想在 table 中出现重复项,您可以尝试:
insert into SUBITEM_TO_ITEM_STATUS (SUBITEM_ID, ITEM_STATUS_ID, CREATED_BY_IDENTIFIER)
select item_subitem_id, 1, creator_identifier
from ITEM_SUBITEMT s
where not exists (select 1
from SUBITEM_TO_ITEM_STATUS tis
where tis.item_subitem_id = s.item_subitem_id
);
错误的 MERGE
语句。
试试这个:
MERGE INTO schema.SUBITEM_TO_ITEM_STATUS T
USING (SELECT ITEM_SUBITEM_ID, 1 AS ITEM_STATUS_ID, creator_identifier FROM schema.ITEM_SUBITEMT) S ON S.ITEM_SUBITEM_ID = T.SUBITEM_ID
WHEN NOT MATCHED THEN
INSERT (SUBITEM_ID, ITEM_STATUS_ID, CREATED_BY_IDENTIFIER)
VALUES (S.item_subitem_id, S.ITEM_STATUS_ID, S.creator_identifier);