Ask the Expert

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.


    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: