把一个表的数据插入到另一服务器的相同表结构的一张表中:
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',1reconfigureexec sp_configure 'Ad Hoc Distributed Queries',1reconfiguregoselect * from Product p inner joinopendatasource('SQLOLEDB','Data Source=Macaco-Online;user ID=sa;password=sa密码;').Company.dbo.Product p2on P.PID=p2.PIDgoexec sp_configure 'Ad Hoc Distributed Queries',0reconfigureexec sp_configure 'show advanced options',0reconfigurego