MSSQL 跨服务器进行不同数据库的操作
把一个表的数据插入到另一服务器的相同表结构的一张表中:
insert into opendatasource('SQLOLEDB','Data Source=*.*.*.*;user ID=sa;password=*****;') .dbname.dbo.tablename select top 10 * from dbname.dbo.tablename
如果执行时出现如下提示:
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', see "Surface Area Configuration" in SQL Server Books Online.
此操作因为服务器安全策略被禁止,可以更改设置。
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