在SQL Server 2012中使用MERGE语句时的语法不正确

时间:2019-06-03 01:05:26

标签: sql sql-server

当我使用public static Expression<Func<ApplicationUser, SearchResultItemViewModel>> Projection(int parm) => item => new SearchResultItemViewModel { Id = item.Id, Article = item.FirstName + parm.ToString() }; 语句时,出现“无效语法”错误:

MERGE

在“ RL”旁边出现错误“语法无效”。我在SQL Server 2012上使用的兼容性级别为110。有人知道这是怎么回事吗?

MERGE ResponsibleLawyers AS RL
USING @tempPerson AS TP ON RL.ResponsibleLawyerID = TP.ResponsibleLawyerID

WHEN MATCHED THEN
    UPDATE 
        SET RL.FirstName = TP.FirstName, 
            RL.LastName = TP.LastName, 
            RL.AccountName = TP.AccountName, 
            RL.EmailAddress = TP.EmailAddress, 
            RL.Region = TP.Region, 
            RL.Active = TP.Active 

WHEN NOT MATCHED BY RL THEN
    INSERT (ResponsibleLawyerID, FirstName, LastName, AccountName, EmailAddress, Region, Active) 
    VALUES (TP.ResponsibleLawyerID, TP.FirstName, TP.LastName, TP.AccountName, TP.EmailAddress, TP.Region, TP.Active)

WHEN NOT MATCHED BY SOURCE THEN 
    DELETE 

1 个答案:

答案 0 :(得分:3)

我认为WHEN NOT MATCHED BY RL THEN应该是WHEN NOT MATCHED BY TARGET THEN。您在此处使用SOURCETARGET,而不是表别名。由于许多示例使用SOURCETARGET作为表别名,所以可能会造成混淆。

Reference