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

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.