合并语句问题(特别是更新部分)

时间:2018-08-14 20:34:12

标签: sql sql-server sql-server-2012

我在使我的合并命令起作用时遇到问题。我发现的是,它不允许一次更新多个列。有什么办法解决吗?

出现的错误是

"Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "s.AddressCaption" could not be bound."

我很肯定服务器和列都存在并且已正确命名。如果我尝试删除S.AddressCaption,则下一列是弹出的错误。

它似乎只能识别s.descr

以下是查询:

 MERGE Payor as T
Using dss.RCSQL.dbo.Payors as S
ON t.code = s.code
WHEN MATCHED and (s.descr <> t.descr or s.AddressCaption <> t.[AddressCaption] or S.Address <> t.address or S.address2 <> t.address2 or 
s.city <> t.city or s.state <> t.state or s.zip <> t.zip or s.ContactPhone <> t.ContactPhone or s.systemID <> t.systemID or s.notes <> t.notes or
s.notes <> t.notes or s.fax <> t.fax or s.medicarepart <> t.medicarepart or s.category <> t.category or s.PayorKey <> t.Payorkey)
THEN
UPDATE SET t.descr = s.descr, s.AddressCaption = t.[AddressCaption],  S.Address = t.address,  S.address2 = t.address2,  
s.city = t.city,  s.state = t.state,  s.zip = t.zip,  s.ContactPhone = t.ContactPhone,  s.systemID = t.systemID,  s.notes = t.notes, 
s.notes = t.notes,  s.fax = t.fax,  s.medicarepart = t.medicarepart,  s.category = t.category,  s.PayorKey = t.Payorkey
WHEN NOT MATCHED THEN 
INSERT (code,descr, [AddressCaption], Address, Address2, City, State, Zip, ContactPhone, systemID, Notes, Fax, MedicarePart, category, PayorKey)
VALUES (s.code,s.descr, s.AddressCaption, s.Address, s.Address2, s.City, s.State, s.Zip, s.ContactPhone, s.systemID, 
s.Notes, s.Fax, s.MedicarePart, s.category, s.PayorKey);

1 个答案:

答案 0 :(得分:0)

每Dave.Gugg解决方案应与首先拥有targettable。[attribute],然后再具有sourcetable。[attribute]。

t.col1 = s.col1, t.col2 = s.col2 ...