方法一:利用opendatasource
示例如下【测试通过】:
select * from opendatasource('SQLOLEDB','server=192.168.1.252;uid=linzhi888;pwd=joherl.net;database=iTry86.Quote').[MyDB].dbo.UserInfo
说明:select 字段 from opendatasource('SQLOLEDB','server=服务器;uid=用户名;pwd=密码;database=数据库').数据库.dbo.表名
注意:若提示SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。
找到解决办法
启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1 reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure
使用完成后,关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure
exec sp_configure 'show advanced options',0 reconfigure
方法二:利用链接服务器
//创建链接服务器【在本地服务器创建】
exec sp_addlinkedserver 'linkserver','','SQLOLEDB','192.168.1.252'
-- exec sp_addlinkedserver '链接服务器名称','','SQLOLEDB','远程地址'
//登录链接服务器
exec sp_addlinkedsrvlogin 'linkserver','false',null,'linzhi888','joherl.net'
--exec sp_addlinkedsrvlogin '链接服务器名称','false',null,'用户名','密码'
//查询语句
select * from linkserver.hangqing.dbo.SZ_Ticks
-- select * from 链接服务器名称.数据库.dbo.表名
也可以通过界面添加链接服务器:
某数据库服务器>>服务器对象>>链接服务器>>右键创建链接服务器>>依次填写链接服务器,服务器类型,产品名称【可以随便写】,数据源【IP地址】,
安全性中的使用此安全上下文建立连接【远程服务器登录名,密码】。然后select * from 链接服务器名称.数据库.dbo.表名即可。
注意:跨服务器分布式查询中不允许对象显示具有 CLR 类型的列,如不支持空间数据类型的列。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。