MERGE语句必须以分号(;)结束

时间:2016-06-12 08:58:53

标签: sql-server tsql

我正在使用此代码,但我收到上面的错误消息。为什么? 我还创建了链接服务器到第二台PC,链接名称= 192.168.1.101,用户名= xxxx和密码= xxxx

WITH Source AS 
  ( SELECT id, name FROM Tamio.dbo.memberform WHERE active = 1 or active =0) 
MERGE INTO [192.168.1.101].Imatismos.dbo.customermaster AS target 
  USING Source ON target.id = source.id 
  WHEN MATCHED THEN UPDATE SET target.name = source.name 
  WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name)

2 个答案:

答案 0 :(得分:3)

在Merge语句的末尾添加一个分号!

WITH Source AS 
  ( SELECT id, name FROM Tamio.dbo.memberform WHERE active = 1 or active =0) 
MERGE INTO [192.168.1.101].Imatismos.dbo.customermaster AS target 
  USING Source ON target.id = source.id 
  WHEN MATCHED THEN UPDATE SET target.name = source.name 
  WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name);

答案 1 :(得分:-1)

一种方法是将所有源数据复制到staging table并在之后处理:

1)复制数据

INSERT INTO Imatismos.stg.memberform
SELECT id, name
FROM Tamio.dbo.memberform WHERE active = 1 or active = 0

2)实际合并可以在本地完成(这在[192.168.1.101] .Imatismos上运行)

WITH Source AS 
  ( SELECT id, name FROM stg.memberform) 
MERGE INTO dbo.customermaster AS target 
  USING Source ON target.id = source.id 
WHEN MATCHED THEN UPDATE SET target.name = source.name 
WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name);

一般来说,尽量避免运行查询"加入"在多台服务器上,因为这会导致性能非常差。