If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Sql Update Using Join Or Subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-07, 19:30
db2dcs db2dcs is offline
Registered User
 
Join Date: Feb 2005
Location: United States
Posts: 20
Sql Update Using Join Or Subquery

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

likewise this fails:

UPDATE AAA.TASKTBL A
SET A.TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD)

anyone's assistance will be greatly appreciated
Reply With Quote
  #2 (permalink)  
Old 02-14-07, 00:49
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
The second one should work. What error message are you getting? Did you look up the error message in the Message Reference manual?

If the error is that you are getting multiple rows in the subselect, then this might work:

UPDATE AAA.TASKTBL A
SET A.TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD fetch first 1 rows only)

Or you may have gotten a SQL0407N (Assignment of a NULL value to a NOT NULL column) which means that the corresponding row on the B table could not be found and DB2 tried to use a null value for the update. To fix that problem, try this:

UPDATE AAA.TASKTBL A
SET A.TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD fetch first 1 rows only)
where exists (SELECT 1 FROM BBB.TASKTBL B WHERE B.TASK_CD = A.TASK_CD)

Edit: The above error message -407 is from DB2 LUW, not OS/390, so it might be a little different (or it might be the same).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 02-14-07, 02:18
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
Quote:
Sql Update Using Join Or Subquery
I suppose this is not supported in DB2 V7. If am right in order to use that you have to be in atleast V8.1 New function Mode. Correct me if am wrong
Reply With Quote
  #4 (permalink)  
Old 02-14-07, 03:42
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
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 )
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 02-14-07, 05:03
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Quote:
Originally Posted by db2dcs
UPDATE AAA.TASKTBL A
SET A.TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD)
the above statement might fail due to the correlation character in the update clause. Simply omit it.

UPDATE AAA.TASKTBL A
SET TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD)
Reply With Quote
  #6 (permalink)  
Old 02-14-07, 05:09
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by umayer
the above statement might fail due to the correlation character in the update clause. Simply omit it.

UPDATE AAA.TASKTBL A
SET TASK_DESC =
(SELECT B.TASK_DESC FROM BBB.TASKTBL B
WHERE B.TASK_CD = A.TASK_CD)
That shouldn't be a problem at all.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 02-14-07, 06:49
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Quote:
Originally Posted by stolze
That shouldn't be a problem at all.

a correlation name in the assignment clause is not supported in DB2 V7

( and in V8 CM it depends on the PTFs applied )
Reply With Quote
  #8 (permalink)  
Old 02-14-07, 07:56
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Thanks. I missed that we are talking about V7 here.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 02-14-07, 17:19
db2dcs db2dcs is offline
Registered User
 
Join Date: Feb 2005
Location: United States
Posts: 20
thanks for everyone's help

Thanks to Marcus and others who advised me to stick with the sub-query approach. And thanks to Umayer who advised me to remove the correlation character. Both pieces of advice helped to me get my update to work.

In actuality, my chore is much more complex than the simple example that I posted. It involves multiple columns being updated based on multiple conditions; and all from within a tricky cursor. But I was able to apply your collective advice to my complicated situation and so far my testing has produced the expected results based on everyone's assistance.

Again much thanks to all of you. It is forums like this, and people like you, that provide mankind with excellence.

regards
db2dcs
Reply With Quote
  #10 (permalink)  
Old 02-15-07, 06:28
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
For UPDATE, you could set multiple columns at once:
Code:
UPDATE table
SET (col1, col2, col3) = ( SELECT x, y, z FROM other_table )
WHERE ...
Also, if you don't have too complex computations in your cursor, it may be worthwhile to push the complete cursor stuff to DB2. But then we would need some more details on that logic.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On