为什么此更新语句未得到编译?根据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))

最佳答案

我认为您需要在UPDATE中引用Alias以获得自我JOIN。至少我只是使用自我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))

10-04 19:09