Quote:
|
Originally Posted by db2dcs
I have seemed to have drawn a blank and I do not know why I cannot perform what appears to be a fundamentally simple update statement.
Namely: I am operating in a Z/OS version 7 DB2 environment and am attempting to do an update using either an inner join or correlated query.
Example:
========
Let us assume that we have two similarly structured
tables in the same subsystem but under two different databases:
AAA.TASKTBL unique key is made up of one column: TASK_CD
BBB.TASKTBL unique key is made up of one column: TASK_CD
I want to update a non-key column (TASK_DESC) in AAA.TASKTBL to its
counterpart in BBB.TASKTBL, based on the respective
TASK_CD values being equal.
I have tried this and it failed:
UPDATE AAA.TASKTBL A,
BBB.TASKTBL B
SET A.TASK_DESC = B.TASK_DESC
WHERE A.TASK_CD = B.TASK_CD
|
This can't be done because the table you want to update is read-only. The table we have here is is a joined table of AAA.TASKTBL and BBB.TASKTBL. Therefore, you have to use a non-read-only (updatable) table for the UPDATE statement, e.g. don't use joins.
Quote:
UPDATE AAA.TASKTBL A
SET A.TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD)
|
As Marcus said, this should work, assuming that (a) DB2 for z/OS V7 supports such subselects, and (b) your subselect is a scalar subselect and returns a single row/value only.
Also note that your update statement will update
every row in AAA.TASKTBL and not only those for which a row in BBB.TASKTBL exists. So your both statements do have quite different semantics. To fix that, you'd have to add a WHERE clause for the UPDATE itself:
Code:
UPDATE AAA.TASKTBL AS A
SET A.TASK_DESC = ( SELECT B.TASK_DESC
FROM BBB.TASKTBL AS B
WHERE B.TASK_CD = A.TASK_CD )
WHERE EXISTS ( SELECT 1
FROM BBB.TASKTBL AS B
WHERE B.TASK_CD = A.TASK_CD )