跨服务器连接数据库

时间:2022-09-23 15:38:19
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE PROCEDURE [dbo].[DH_ReceiveFileInserts]
(
@djh NVARCHAR(50) ,
@djrq datetime ,
@flh NVARCHAR(50),
@wh NVARCHAR(50),
@wjbt NVARCHAR(50),
@zrz NVARCHAR(50),
@bgqx NVARCHAR(50),
@mj NVARCHAR(50),
@cwrq datetime,
@ztc NVARCHAR(50),
@ys NVARCHAR(50),
@fs NVARCHAR(50),
@qwbs NVARCHAR(50),
@hjcd NVARCHAR(50),
@bz NVARCHAR(50),
@cbjg NVARCHAR(50),
@wb NVARCHAR(50),
@lwdw NVARCHAR(50),
@cjyqk NVARCHAR(50),
@cjybs NVARCHAR(50)
)
AS
SET NOCOUNT OFF
INSERT INTO ITSV.GJJOA.dbo.wj14
(djh,djrq,flh,wh, wjbt, zrz,bgqx, mj, cwrq, ztc, ys, fs,qwbs,hjcd,bz,cbjg,wb,lwdw,cjyqk,cjybs, fj, jg_id, bm_id,
sign, dalx )
values (@djh, @djrq, @flh, @wh, @wjbt, @zrz,@bgqx, @mj, @cwrq, @ztc, @ys, @fs,@qwbs,@hjcd,@bz,@cbjg,@wb,@lwdw,@cjyqk,@cjybs, '', 0, 0, 'f', 'wj1429')
exec sp_dropserver 'ITSV','droplogins'
exec sp_addlinkedserver 'ITSV','','SQLOLEDB','10.10.1.17'
exec sp_addlinkedsrvlogin 'ITSV','false',null,'sa','ntgjj'

//创建链接
exec sp_addlinkedserver '别名','','SQLOLEDB','服务器地址'
exec sp_addlinkedsrvlogin '别名','false',null,'登录名','密码'
//删除链接
exec sp_dropserver '别名','droplogins'
//使用
select * from 别名.数据库名.dbo.表名