我被表面上看起來很容易的Update語句所困惑。我需要使用Table2中的值更新Table1中4列的值,這是基於每個表中匹配兩個單獨的列。這裏是一般語法:SQL更新語句正在更新
Update Table1
set field1 = [table2 field1]
, field2 = [table2 field2]
, field3 = [table2 field3]
, field4 = [table2 field4]
--select *
from Table2 p inner join Table1 c
on p.matchfield1 = c.matchfield1 and p.matchfield2 = c.matchfield2
where c.database_id = 1 --qualifier since join clause may not provide unique result set
and p.matchfield1 = 123456 --to test the statement on a single record first
當我作爲select語句運行子句時,我得到一個單一的記錄按預期。但是,當我使用相同的'from'語言運行更新子句時,我發現查詢使用Table2中的值更新了Table1中的每一行,其中matchfield1 = 123456.
我想知道是否需要進一步澄清我where條款基於matchfield2值(在我的示例中是eamil地址)進行限定,所以我將添加到語句末尾'和p.matchfield2 ='[email protected]''。
我懷疑我是一個非常小的調整,而不是做這項工作,但同時這讓我瘋狂。感謝您的任何建議!
你使用了哪個數據庫? –
我正在使用SQL Server 2008.對不起,我沒有添加到最初的帖子。 – user3167010