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 > need help with multi-table update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-07-07, 15:04
amy7756 amy7756 is offline
Registered User
 
Join Date: Jul 2006
Posts: 28
need help with multi-table update

Hey All -

I need to update a table from data in another table. IE:

table A:

name, date

table B:

name, date

I need to update all the dates in table b from table a where a.name=b.name.

Thanks in advance!

Amy
Reply With Quote
  #2 (permalink)  
Old 03-08-07, 07:51
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
UPDATE b
SET b.date = ( SELECT date FROM a WHERE b.name = a.name )
WHERE EXISTS ( SELECT date FROM a WHERE b.name = a.name )
Note that there must be at most one tuple in table A for each tuple in table B. Otherwise, you will have to specify which row in A should be used to update a row in B.

Specific products may have other facilities like MERGE statements or the ability to update something based on an updatable result set. For example:
Code:
UPDATE ( SELECT b.name, b.date, ( SELECT a.date FROM a WHERE a.name = b.name ) AS a_date FROM b )
SET date = a_date
WHERE a_date IS NOT NULL
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 03-08-07, 07:57
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Here's your first hint Let me know how you get on!
Code:
UPDATE table-name SET column-name = {expression} [, column-name = {expression}] WHERE search-condition
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 03-08-07, 08:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
amy, please do mention which database system you're using

the update SQL is different from one to the next
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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