DB2 Update with join queries

点点圈 提交于 2019-12-06 13:25:35
Hemant

Use a merge query to update the table, instead of join. DB2 does not accept join in update query for that purpose, you have to use a merge:

MERGE INTO TABLE_NAME1 A 
    USING (SELECT  COL1, COL2  FROM TABLE_NAME2) B
    ON A.COL1 = B.COL2
    WHEN MATCHED AND A.COL1 = B.COL2
    THEN UPDATE SET A.COL1 = B.COL2;

Does your 1st query not work? I'm not familiar with comma-separating parts of the WHERE clause (it's not valid on my version of DB2 - is it actually part of the syntax?).

Normally, when I need to run these types of update queries, I use an EXISTS clause, like this:

UPDATE data as a SET b_desc = p_desc, p_desc = null
WHERE EXISTS (SELECT '1'
              FROM c_data as b
              WHERE b.b_desc = a.b_desc
              AND b.p_desc = a.p_desc)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!