简体   繁体   English

合并或插入忽略 db2

[英]merge into or insert ignore for db2

I feel like this should be fairly straightforward, but I'm having an issue figuring out the best way to do this我觉得这应该相当简单,但我在找出最佳方法时遇到了问题

I have a number of subitems in this subitem_to_item_status table that have a status but I need to insert a record for all subItems that aren't there yet.我在这个 subitem_to_item_status 表中有许多具有状态的子项,但我需要为所有尚不存在的子项插入一条记录。 I don't need to update what's in there, I'm really just trying to replicate an 'insert/ignore'我不需要更新里面的内容,我真的只是想复制一个“插入/忽略”

Sample Data for subitems子项的示例数据

ITEM_SUBITEMT 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_TO_ITEM_STATUS

SUBITEM_ID   |   ITEM_STATUS_ID   |   CREATED_BY_IDENTIFIER   
------------------------------------------------------------
12                  1                   12345
15                  1                   12345
16                  1                   12345
20                  1                   12345

So with that data, I just need to insert records from the first table that don't have the id 12,15,16 or 20所以有了这些数据,我只需要插入第一个表中没有 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;

Wrong MERGE statement.错误的MERGE语句。
Try this:尝试这个:

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);

Why not use 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);

If what you mean is that you don't want duplicates in the table, you can try:如果你的意思是你不想在表中重复,你可以尝试:

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
                    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM