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 > Data Access, Manipulation & Batch Languages > ANSI SQL > [DB2] Update Field in Table A to a Field in Table B

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-04, 11:07
d_lynch d_lynch is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
[DB2] Update Field in Table A to a Field in Table B

I am trying to change the values in Field2 of table A to the values in Field2 of table B wherever the tables have the same value for Field1.

This works in Microsoft Access, but I can't figure out how to make it work in DB2.

UPDATE A INNER JOIN B ON A.Field1 = B.Field1 SET A.Field2 = [B].[Field2];

Any ideas?

Last edited by d_lynch; 12-02-04 at 11:12.
Reply With Quote
  #2 (permalink)  
Old 12-02-04, 15:20
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
I know this works in Oracle, but not tested in DB2...

Code:
UPDATE a
SET a.field2 = NVL( ( SELECT b.field2
                             FROM b
                             WHERE b.field1 = a.field1), a.field2);
__________________
JoeB
save disk space, use smaller fonts
Reply With Quote
  #3 (permalink)  
Old 12-02-04, 15:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
try this (untested; i don't have DB2, but i know it allows scalar subqueries in the UPDATE statement) --
Code:
update A 
   set Field2 
     = ( select Field2
           from B
          where Field1 = A.Field1 )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 12-02-04, 15:51
d_lynch d_lynch is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks, I just replaced NVL with Coalesce and it worked fine. I greatly appreciate the help.
Reply With Quote
  #5 (permalink)  
Old 12-03-04, 00:34
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
Rudy,

Just so you know... I tried your variation in an attempt to find a solution for d_lynch... problem I found was that:

Code:
update A 
   set Field2 
     = ( select Field2
           from B
          where Field1 = A.Field1 )
...works for the fields that have a match, however, if there is no match, whatever was in A.FIELD2 is now replaced with a NULL.
__________________
JoeB
save disk space, use smaller fonts
Reply With Quote
  #6 (permalink)  
Old 12-03-04, 05:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
thanks, joe, i understand that

i wouldn't update A.Field2 with itself, though -- could be lotsa useless log activity

i'd use a WHERE clause to ensure that only those rows which had a match are actually updated
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-03-04, 09:40
joebednarz joebednarz is offline
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
Cool... not really into correcting other people's code, but had tried it so I thought I'd mention the results. BTW, always appreciate your answers to questions... very well thought out.
__________________
JoeB
save disk space, use smaller fonts
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