MERGE语句的语法不正确

时间:2014-01-16 18:47:28

标签: sql

我正在尝试从临时表中将MERGE导入我们的常规表。但是,SQL Server Management Studio在ZipCodeTerritory语句中的UPDATE表名称上显示错误。

我之前没有写过MERGE,但我无法看到下面的语法有什么问题。由于智能感知错误报告可能与Management Studio非常具有描述性,因此其他任何人都可以看到下面的查询有什么问题吗?

MERGE INTO ZipCodeTerritory as Target
USING ZipCodeTerritoryTemp as Source
ON
Target.Id = Source.Id

WHEN MATCHED THEN

UPDATE ZipCodeTerritory SET Target.ChannelCode = Source.ChannelCode, Target.DrmTerrDesc = Source.DrmTerrDesc, Target.IndDistrnId = Source.IndDistrnId,
    Target.StateCode = Source.StateCode, Target.ZipCode = Source.ZipCode, Target.EndDate = Source.EndDate, Target.EffectiveDate = Source.EffectiveDate,
    Target.LastUpdateId = Source.LastUpdateId, Target.LastUpdateDate = Source.LastUpdateDate, Target.ErrorCodes = Source.ErrorCodes,
    Target.Status = Source.Status 
WHERE Target.Id = Source.Id

WHEN NOT MATCHED THEN

INSERT INTO ZipCodeTerritory (ChannelCode, DrmTerrDesc, IndDistrnId, StateCode, ZipCode, EndDate, EffectiveDate, LastUpdateId, LastUpdateDate, ErrorCodes, Status) 
VALUES(Source.ChannelCode, Source.DrmTerrDesc, Source.IndDistrnId, Source.StateCode, Source.ZipCode, Source.EndDate, Source.EffectiveDate, Source.LastUpdateId, Source.LastUpdateDate, Source.ErrorCodes, Source.Status)

1 个答案:

答案 0 :(得分:2)

Check syntaxUpdateInsert子句没有表名。它也不是Merge Into简单Merge

MERGE ZipCodeTerritory as Target
   ...
WHEN MATCHED THEN    
   UPDATE SET Target.ChannelCode = Source.ChannelCode ...
WHEN NOT MATCHED THEN 
   INSERT (Ch ...

为什么呢?表名是多余的,因为它在Merge子句中被告知。