Q

Updating a table by adding data from another table

Can I update a table by adding data from another table?

TABLE1 s_ID, s_name, ownerID
120   NAME    <<data here>>
TABLE2
ownerID, s_name
650      NAME NAME
I want to get the (new table association) TABLE1.ownerID rows populated with the values from TABLE2.ownerID. I need somthing like:
update TABLE1
set ownerID = table2.ownerID
where table1.s_name + ' String' = table2.s_name
I'm using SQL Server 7.0 AND SQL Server 2000.


Here, I would use a simple correlated sub-query.

update TABLE1
  set OwnerID = ( select OwnerID from TABLE2 
                    where s_name = table1.s_name + 
                                   'String');
For more information, you can see the responses I posted to similar questions at http://searchdatabase.techtarget.com/ateAnswers/0,289620,sid13_cid412523_tax289340,00.html.

For More Information


This was first published in September 2002

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close