从 SQL Server 存储过程结果中选择
我要将数百个存储过程从一台服务器迁移到另一台服务器,因此我想编写一个存储过程来在每台服务器上执行 SP 并比较输出的差异。
为了做到这一点,我通常会使用此语法将结果放入表中:
select * into #tmp1 from OpenQuery(LocalServer,'exec usp_MyStoredProcedure')
select * into #tmp2 from OpenQuery(RemoteServer,'exec usp_MyStoredProcedure')
然后我将它们合并并进行计数,以获取结果中有多少行不同:
select * into #tmp3
from ((select * from #tmp1) union (select * from #tmp2))
select count(*) from #tmp1
select count(*) from #tmp3
但是,在这种情况下,我的存储过程包含一个OpenQuery
,因此当我尝试将 exec
放入 OpenQuery
中时,查询失败并出现错误:
The operation could not be performed because OLE DB provider "SQLNCLI"
for linked server "RemoteServer" was unable to begin a distributed transaction.
对此有什么好的解决方法吗?或者有人对我可以做些什么来让这个过程进行得更快有什么聪明的想法吗?因为现在,我似乎必须在每台服务器上运行 SP,将结果编写到 tmp 表中,然后进行比较。这似乎是一个糟糕的解决方案!
感谢您花时间阅读本文,如有任何帮助,我们将不胜感激!
I am migrating several hundred stored procedures from one server to another, so I wanted to write a stored procedure to execute an SP on each server and compare the output for differences.
In order to do this, I would normally use this syntax to get the results into tables:
select * into #tmp1 from OpenQuery(LocalServer,'exec usp_MyStoredProcedure')
select * into #tmp2 from OpenQuery(RemoteServer,'exec usp_MyStoredProcedure')
I then would union them and do a count, to get how many rows differ in the results:
select * into #tmp3
from ((select * from #tmp1) union (select * from #tmp2))
select count(*) from #tmp1
select count(*) from #tmp3
However, in this case, my stored procedure contains an OpenQuery
, so when I try to put the exec
into an OpenQuery
, the query fails with the error:
The operation could not be performed because OLE DB provider "SQLNCLI"
for linked server "RemoteServer" was unable to begin a distributed transaction.
Are there any good workarounds to this? Or does anybody have any clever ideas for things I could do to make this process go more quickly? Because right now, it seems that I would have to run the SP on each server, script the results into tmp tables, then do the compare. That seems like a poor solution!
Thank you for taking the time to read this, and any help would be appreciated greatly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的方法会起作用 - 你只需要启动 MSDTC 即可。如果禁用分布式事务协调器 (DTS) 服务或禁用网络 DTC 访问,则会出现此行为。默认情况下,Windows 中禁用网络 DTC 访问。当正确运行和配置时,OLE DB 提供程序将能够启动分布式事务。
查看此说明 - 它适用于任何 Windows Server 2003 或 2008。
I think your method would work - you just need to start the MSDTC. This behavior occurs if the Distributed Transaction Coordinator (DTS) service is disabled or if network DTC access is disabled. By default, network DTC access is disabled in Windows. When running and configured properly, the OLE DB provider would be able start the distributed transaction.
Check out this for instructions- it applies to any Windows Server 2003 or 2008.
和你的问题类似。
将存储过程的结果插入临时表
Similar to your question.
Insert results of a stored procedure into a temporary table