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 )
跨数据库服务器查询

相关文章:

  • 2021-09-27
  • 2021-11-18
  • 2021-12-20
  • 2021-12-19
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-27
  • 2022-03-08
  • 2021-11-22
  • 2022-12-23
  • 2022-02-15
相关资源
相似解决方案