我正在尝试使用merge从链接服务器插入记录。该表只有两列,都需要使用这两列来检查新记录。
MERGE dbo.[TableA] AS Target
USING (SELECT [PERMNUM], [ABS_DATE]
FROM [Linked Server].[dbo].[TableA])
AS source ([PERMNUM], [ABS_DATE]
ON (Target.[PERMNUM] = source.[PERMNUM]
AND Target.[ABS_DATE] = source.[ABS_DATE])
WHEN NOT MATCHED THEN
INSERT ([PERMNUM], [ABS_DATE])
VALUES (source.[PERMNUM], source.[ABS_DATE]);
我收到以下错误。
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'ON'.
如何在ON子句中使用这两列?我看过其他示例,据我所知我是正确的。
最佳答案
好吧,好像我缺少一个')'
我修复了代码,现在它是正确的:
SET NOCOUNT ON
MERGE dbo.[PSPI_DAY_ABSENCES] AS Target
USING (SELECT [PERMNUM], [ABS_DATE]
FROM [SQLPASS01].[PSPI].[dbo].[PSPI_DAY_ABSENCES])
AS source ([PERMNUM], [ABS_DATE])
ON Target.[PERMNUM] = source.[PERMNUM] AND Target.[ABS_DATE] = source.[ABS_DATE]
WHEN NOT MATCHED THEN
INSERT ([PERMNUM], [ABS_DATE])
VALUES (source.[PERMNUM], source.[ABS_DATE]);