加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 在链接服务器上执行SP并将其放在临时表中

发布时间:2020-12-12 06:47:00 所属栏目:MsSql教程 来源:网络整理
导读:需要一些关于以下问题的帮助: 案例1:存储过程在服务器1上 – 来自server1的调用 declare @tempCountry table (countryname char(50))insert into @tempCountry exec [database1_server1].[dbo].[getcountrylist]Select * from @tempCountry 结果:成功执行
需要一些关于以下问题的帮助:

案例1:存储过程在服务器1上 – 来自server1的调用

declare @tempCountry table (countryname char(50))
insert into @tempCountry
    exec [database1_server1].[dbo].[getcountrylist]
Select * from @tempCountry

结果:成功执行

案例2:如果使用链接服务器从不同的服务器调用相同的存储过程,如下所示:

declare @tempCountry table (countryname char(50))
insert into @tempCountry
    exec [database2_server2].[database1_server1].[dbo].[getcountrylist]
Select * from @tempCountry

结果

Msg 7391,level 16,state 2,line 2
The operation could not be performed because OLEDB provider “SQLNCLI” for linkedserver “Server2_Database2” was unable to begin a distributed transaction.

案例3

但是当试图单独执行存储过程[没有临时表插入]如下所示

exec [database2_server2].[database1_server1].[dbo].[getcountrylist]

结果:正在执行存储过程而没有任何错误并返回数据.

我忘了提到我使用的是SQL Server 2005.根据服务器管理员的说法,我建议使用的功能在2005年不可用.

解决方法

你有(我相信)两个选择:

>尝试通过使用OPENQUERY行集函数来避免使用MSDTC(以及与分布式事务相关的所有these不愉快的事情)

/假设(此处和下面)[database2_server2]是链接服务器的名称/

声明@tempCountry表(countryname char(50))
插入@tempCountry
select * from openquery([database2_server2],'[database1_server1].[dbo].[getcountrylist]’)
从@tempCountry中选择*

要么

>您可以将链接服务器的选项Enable Promotion Of Distributed Transaction设置为False,以防止本地事务提升分布式事务,从而使用MSDTC:

EXEC master.dbo.sp_serveroption
@server = N’database2_server2′,
@optname = N’remote proc transaction promotion’,
@optvalue = N’false’

并且您的原始查询应该正常工作:

声明@tempCountry表(countryname char(50))插入@tempCountryexec [database2_server2].[database1_server1].[dbo].[getcountrylist]从@tempCountry中选择*

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读