将表信息从一台服务器移动到另一台服务器的过程不起作用SQL

时间:2019-02-25 09:11:02

标签: sql sql-server stored-procedures

我正在尝试创建一个过程,以将信息从服务器连接移至我创建的本地数据库。 我的本地数据库名称为Test,我们有一台服务器,通过一些通用凭据连接到该服务器,由于该服务器也链接到数据库,因此我使用以下查询来选择我的信息。 当我将主服务器与以下查询一起使用时,我会得到所需的列:

select * from openquery(servicenow, 'SELECT  name, user_name, company,    business_unit, location,manager, email,city, u_world_area, country, Street,    active from sys_user'    

到目前为止,我已经创建了以下过程:

use Test

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create procedure [dbo].[Update_location] 
as
begin
WITH F AS (
select 
name,user_name, company, u_business_unit, location, manager, email, city,
u_world_area, country, Street, active     
FROM sys_user
),
O AS ( 
select * from openquery(servicenow, 'SELECT 
name, user_name, company, u_business_unit, location, manager,  email,  city, u_world_area, country, Street, active
from sys_user')
)
MERGE INTO F USING O ON F.user_name = O.user_name

WHEN NOT MATCHED BY TARGET THEN  
INSERT (name, user_name, company, u_business_unit, location, manager,  email, city, u_world_area, country, Street, active) 
VALUES (name, user_name, company, u_business_unit, location, manager,  email, city, u_world_area, country, Street, active)

 WHEN MATCHED AND (f.location <> o.location) or (f.manager <> o.manager) then 
 UPDATE SET f.location = o.location, f.manager = o.manager

WHEN NOT MATCHED BY SOURCE THEN DELETE;
end;

我知道需要添加一些东西才能使其正常工作,但我不知道是什么和如何操作,当前出现此错误:

  

消息7202,第11级,状态2,过程Update_location,第5行[批处理   第6行开始]

     

在sys.servers中找不到服务器“ servicenow”。验证   指定了正确的服务器名称。如有必要,执行存储的   过程sp_addlinkedserver将服务器添加到sys.servers。

提前谢谢!

0 个答案:

没有答案