OPENDATASOURCE

 

SQL SERVER 访问远程DB(不能用Linkserver)可以采用OPENDATASOURCE方式,但是风险未知。

 

 

调用方式:

SELECT top 10 *  
FROM OPENDATASOURCE('SQLNCLI',  
    'Data Source=ip,1433;uid=zhanghao;pwd=mima;database=DB')  
    .DB.[dbo].[tbs]  

 

需要开启一项配置,

--enable
exec sp_configure 'show advanced options',1  
reconfigure  
exec sp_configure 'Ad Hoc Distributed Queries',1  
reconfigure 

--disable
exec sp_configure 'Ad Hoc Distributed Queries',0 
reconfigure 
exec sp_configure 'show advanced options',0 
reconfigure

 

否则报错

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries'
because this component is turned off as part of the security configuration for this server.
A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure.
For more information about enabling 'Ad Hoc Distributed Queries', search for 'Ad Hoc Distributed Queries' in SQL Server Books Online.

 

 

参考文献:https://www.sqlshack.com/querying-remote-data-sources-in-sql-server/

 

posted @ 2020-03-16 10:54  JinweiChang  阅读(254)  评论(0编辑  收藏  举报