跨数据库查询
if not exists(select * from Sys.servers where name='srvname')
begin
exec sp_addlinkedserver 'srvname','','SQLOLEDB','127.0.0.1'
exec sp_addlinkedsrvlogin 'srvname','false',null,'sa','123456'
EXEC master.dbo.sp_serveroption @server=N'srvname', @optname=N'rpc', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'srvname', @optname=N'rpc out', @optvalue=N'true'
end
go
--查询示例
select * from srv_lnk.xgoss_yhyd.dbo.temp
--exec sp_dropserver 'srv_lnk','droplogins' --删除连接