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 using joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-22-09, 05:38
dinjo_jo dinjo_jo is offline
Registered User
 
Join Date: May 2008
Posts: 34
Update using joins

I need to perform a update but not able to figure out in DB2


Code:
update TABLE A 
set B.COL = 6794
from TABLE B
where A.id = B.id 
and B.NAME = 'ABC'
and date(B.STARTDATE) = date (current date)
Reply With Quote
  #2 (permalink)  
Old 12-22-09, 06:17
kandrusatish kandrusatish is offline
Registered User
 
Join Date: Jun 2008
Location: Chennai
Posts: 21
Try this

update TABLE B
set B.COL = 6794
where B.id IN (select id from TABLE A)
and B.NAME = 'ABC'
and date(B.STARTDATE) = date (getdate())
Reply With Quote
  #3 (permalink)  
Old 12-22-09, 06:21
dinjo_jo dinjo_jo is offline
Registered User
 
Join Date: May 2008
Posts: 34
What ? I said table a needs to be updated ?
Reply With Quote
  #4 (permalink)  
Old 12-22-09, 06:57
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
dinjo jo, Your statement is confusing, You have UPDATE TABLE A but it is followed by SET B.COL = 6794. It is not easy to tell which Table you were trying to Update.
Reply With Quote
  #5 (permalink)  
Old 12-22-09, 10:31
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
either way, you would still write your sql in the manner above just switch your tables around.
Dave
Reply With Quote
  #6 (permalink)  
Old 12-22-09, 12:51
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
you have lost the join condition - you take the complete table a
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #7 (permalink)  
Old 12-22-09, 17:54
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Following to the syntax of UPDATE statement,
"update TABLE A ..." means that you want to update a table named "TABLE" and rename it A for referencing the table later in the statement.

It would be better to show example(sample data of tables before and after updated) to describe what you want to do.
Reply With Quote
  #8 (permalink)  
Old 12-22-09, 23:28
kandrusatish kandrusatish is offline
Registered User
 
Join Date: Jun 2008
Location: Chennai
Posts: 21
i assume the following code is what you are expecting

update TABLE A
set A.COL = 6794

where A.id IN

(select id from TABLE B
where B.NAME = 'ABC'
and date(B.STARTDATE) = date (getdate())
)


And if you are sure that the ID column is having unique values, then you can replace the operator 'IN' with '=' in my code, otherwise use the code as it is
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