跨数据库查询

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' --删除连接

posted on 2012-11-16 16:04  oer  阅读(219)  评论(0编辑  收藏  举报

导航