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 > UPDATE sql - INNER JOIN

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-12-06, 00:37
chas_dba chas_dba is offline
Registered User
 
Join Date: Sep 2003
Posts: 33
Red face UPDATE sql - INNER JOIN

In DB2/ZOS/os390 will the inner join works in an update statement
UPDATE S01.CLNT_T CAR
INNER JOIN S01.ADDR_T ADR
ON CAR.ADDR_ID = ADR.ADDR_ID
SET CAR.ZIP = SUBSTR(ADR.ZIP,1,5)
WHERE CAR.KEY = 1;

If yes.. will the above statement work? I have got some sql error working with this.. any help is appreciated.
__________________
bs
Reply With Quote
  #2 (permalink)  
Old 04-12-06, 02:33
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
No, you can't use a join in that way for an update.

Try this instead:

UPDATE S01.CLNT_T CAR
SET CAR.ZIP =
( SELECT SUBSTR(ADR.ZIP,1,5)
FROM S01.ADDR_T ADR
WHERE ADR.ADDR_ID = CAR.ADDR_ID )
WHERE CAR.KEY = 1;
Reply With Quote
  #3 (permalink)  
Old 04-13-06, 00:57
chas_dba chas_dba is offline
Registered User
 
Join Date: Sep 2003
Posts: 33
Thanks a lot umayer.

If i need to update more than one column , do i need to add another sub query for that variable is it.
__________________
bs
Reply With Quote
  #4 (permalink)  
Old 04-13-06, 03:07
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
There are two alternatives to update more than one row:

you can use more than one assignment clauses:

UPDATE table
SET column1 = ( SELECT col1 FROM .... ) ,
column2 = ( SELECT col2 FROM .... )
WHERE ....


or you can bracket the columns:

UPDATE table
SET ( column1 , column2 ) = ( SELECT col1, col2 FROM .... )
WHERE ...
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