各位用户为了找寻关于sqlserver 多表查询不同数据库服务器上的表的资料费劲了很多周折。这里教程网为您整理了关于sqlserver 多表查询不同数据库服务器上的表的相关资料,仅供查阅,以下为您介绍关于sqlserver 多表查询不同数据库服务器上的表的详细内容
第一种方法:
代码如下:
/* 创建链接服务器 */ 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