现在的位置: 首页 > 综合 > 正文

SQL跨服查询

2013年05月10日 ⁄ 综合 ⁄ 共 533字 ⁄ 字号 评论关闭

这时才想起跨库查询了。

1.开通分布式查询权限

exec sp_configure 'show advanced options',1
reconfigure

exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure

2.查询

select a.ProjCode as '楼盘编号',b.projname as '楼盘名称',count(1) as '房源量' from
 openrowset( 'SQLOLEDB ', 'IP地址(服务器名)'; '用户名'; '密码',[数据库名].[dbo].[表名]) a
 , openrowset( 'SQLOLEDB ', 'IP地址(服务器名)'; '用户名'; '密码',[数据库名].[dbo].[表名]) b
 where a.ProjCode=b.newcode GROUP by   a.ProjCode ,b.projname order by  count(1) desc

 

3.然后换城市,只需要改IP、 库名和表名了。

4.关闭

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

抱歉!评论已关闭.