Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The table 'dbo.UserProperties' is ambiguous. Why this error is coming?

Tags:

sql

sql-server

Why this update statement is not getting compiled ? According to the msdn it is perfectly right.

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))
like image 709
teenup Avatar asked Jul 05 '10 07:07

teenup


1 Answers

I think you need to reference the Alias in the UPDATE for a self JOIN. At least I just tried an UPDATE with a self JOIN and the following kind of syntax worked for me.

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))
like image 107
Martin Smith Avatar answered Nov 10 '22 06:11

Martin Smith