Click here to Skip to main content
14,356,475 members
Rate this:
Please Sign up or sign in to vote.
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.

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

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this:
UPDATE table1
    table1.firstname = table2.firstname,
    table1.lastname = table2.lastname, =
    table1 JOIN table2
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100