I am using DB2 for performing the below update operation.
update DATA set B_DESC=P_DESC, P_DESC=null
where B_DESC= *, P_DESC=*
(Select B_DESC,P_DESC from C_DATA)
The below is actually possible but since complex joins are involved in that sub query it is not advisable to use like below
update DATA set B_DESC=P_DESC, P_DESC=null
where B_DESC= (Select B_DESC from C_DATA), P_DESC=(Select P_DESC from C_DATA)
I have to update DATA table, but the B_DESC and P_DESC i have to fetch it from C_DATA table and use it in the UPDATE query.
Please let me know how to do it. It has to be a single query if possible.
Thanks in advance.
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)