第一种方法:
/* 创建链接服务器 */
exec sp_addlinkedserver 'srv_lnk','','sqloledb','条码数据库IP地址'
exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'用户名','密码'
go
/* 查询示例 */
SELECT A.ListCode
FROM srv_lnk.条码数据库名.dbo.ME_ListCode A, IM_BarLend B
WHERE A.ListCode=B.ListCode
go
/* 删除链接服务器 */
exec sp_dropserver 'srv_lnk','droplogins'
第二种方法:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
go
select * from Product p inner join
opendatasource('SQLOLEDB','Data Source=Macaco-Online;user ID=sa;password=sa密码;').Company.dbo.Product p2
on P.PID=p2.PID
go
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
go