SQl Server的更新语句不起作用 [英] Update statement for SQl Server is not working
问题描述
我有一个SQL Server 2005.我有两个表。 Table1是旧表,table2是更新表。我试图用table2数据更新table1但我的更新代码是错误的。 table1中的内容只是名字,姓氏,电子邮件,访问级别。 Table2具有更新的First,Last和Email。我只想更新名字,姓氏和电子邮件,而不会丢失table1中的访问级别。此外,表中还包含用户标识。我试图用table2数据更新table1,具体取决于用户ID。这是我的代码。
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] =
Table1.userid
FROM Table2
WHERE [Hot].[dbo].[Table2].userid = Table2.userid
推荐答案
试试这个:
Try this:
UPDATE table1
SET
table1.firstname = table2.firstname,
table1.lastname = table2.lastname,
table1.email = table2.email
FROM
table1 JOIN table2
ON
table1.userid=table2.userid
这篇关于SQl Server的更新语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!