Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a SQL Server 2005. I have two tables. Table1 is the old table and table2 is the updated table. I am trying to update table1 with table2 data but my update code is wrong. What I have in table1 is just Firstname , Lastname, Email, accesslevel. Table2 has the updated First, Last and Email. I just want to update the Firstname, Lastname and Email without loosing the accesslevel in table1. Also the tables have userid's in them also. I was trying to update table1 with table2 data depending on the userid. Here is my code.

SQL
UPDATE [Hot].[dbo].[Table1] SET [Firstname], [Lastname], [Email] = 
Table1.userid
FROM Table2
WHERE [Hot].[dbo].[Table2].userid = Table2.userid
Posted

1 solution

Try this:
SQL
UPDATE table1
SET
    table1.firstname = table2.firstname,
    table1.lastname = table2.lastname,
    table1.email = table2.email
FROM
    table1 JOIN table2
ON
    table1.userid=table2.userid
 
Share this answer
 
v3
Comments
Computer Wiz99 30-Jun-14 10:35am    
Peter Leow, Thanks for the code but will this update the right recodes in the table that matches the userid and accesslevel?
Peter Leow 30-Jun-14 11:53am    
yes, for records that have the same userid in both tables. The accesslevel will not be changed.
Computer Wiz99 30-Jun-14 16:02pm    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900