By Richard Tsuis, http://richardtsuis.cnblogs.com/
This posting is provided "AS IS" with no warranties, and confers no rights.

跨数据库服务器查询

方法1
直接使用UNC网络地址名称指定访问路径(不推荐使用)
例如:

Select col1 from UNCName.DB1.dbo.Table1
      
Where ID1=(select top 1 ID2 from table2 where ID2=@intUID)

问题:这样的查询可能使用相对指定的方式(NetBIOSTCP/IP)去进行查询,可能不能体现出数据库引擎的传输优势。而且直接查询也可能是导致问题的原因,实际应该通过存储过程准备好数据集合后再进行查询。

方法2

通过OPENROWSET函数完成数据库服务器联接和查询操作,准备好数据集合。
当然这种情况下所使用的数据集合是只读的,更新方面可以考虑另外的控制方法,在数据库里可以通过方法1进行,在程序里可以通过数据库连接区别操作:

Declare @emptyContent TABLE
(
    recordID 
uniqueidentifier,
    content 
nvarchar(255)
)

Insert Into @emptyContent (recordID, content)

Select local.recordID, local.content
FROM OPENROWSET('SQLOLEDB','remoteserver';'accountname';'password',
   
'SELECT recordID, content From database.dbo.table Where content = '''' 'AS local

Select gh.recordID, gh.content
From table gh
Where content = ''
And Not Exists(Select * From @emptyContent ec Where ec.recordID = gh.recordID )
Posted on 2007-06-21 08:22  Richard Tsuis  阅读(1366)  评论(1编辑  收藏  举报