为什么没有编译此更新语句?根据msdn,这是完全正确的。
UPDATE dbo.UserProperties
SET Value = MergeFrom.Value
FROM dbo.UserProperties MergeFrom ,
dbo.UserProperties MergeTo
WHERE MergeFrom.Field = MergeTo.Field
AND MergeTo.[Key] = CAST(@MergeToUserID AS NVARCHAR(50))
AND MergeFrom.[Key] = CAST(@MergeFromUserID AS NVARCHAR(50))
答案 0 :(得分:10)
我认为您需要在UPDATE中引用自我JOIN的Alias。至少我刚尝试了一个带有自我JOIN的UPDATE,并且以下类型的语法对我有效。
UPDATE MergeTo
SET MergeTo.Value = MergeFrom.Value
FROM dbo.UserProperties AS MergeFrom INNER JOIN
dbo.UserProperties AS MergeTo ON MergeFrom.Field = MergeTo.Field
WHERE MergeTo.[Key] = CAST(@MergeToUserID AS NVARCHAR(50))
AND MergeFrom.[Key] = CAST(@MergeFromUserID AS NVARCHAR(50))