链接的服务器提交事务尝试尝试失败

时间:2018-12-26 09:24:45

标签: sql-server linked-server

我正在使用Linked Server,并且此代码工作正常

BEGIN TRY
    INSERT INTO [Dev].[dbo].tb_test (no)  SELECT no from [MYLINKEDSERVER].[mydb].[dbo].tb_test 
    DELETE FROM [MYLINKEDSERVER].[mydb].[dbo].tb_test
END TRY
BEGIN CATCH
    SELECT 'fail'
END CATCH

但是当我将其与COMMIT TRANSACTION一起使用时,就像这样

BEGIN TRY
    BEGIN TRANSACTION 
    INSERT INTO [Dev].[dbo].tb_test (no)  SELECT no from [MYLINKEDSERVER].[mydb].[dbo].tb_test 
    DELETE FROM [MYLINKEDSERVER].[mydb].[dbo].tb_test
    COMMIT
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK
END CATCH

显示错误

  

链接服务器“ MYLINKEDSERVER”的OLE DB提供程序“ SQLNCLI11”返回了消息   “合作伙伴交易经理已停用了对   远程/网络事务。”。

怎么了?

3 个答案:

答案 0 :(得分:1)

解决方案是使用返回值在远程服务器上编写存储过程,并在本地执行它:

BEGIN TRY
BEGIN TRANSACTION 
    INSERT INTO [Dev].[dbo].tb_test (no)  
        SELECT no 
        FROM [MYLINKEDSERVER].[mydb].[dbo].tb_test 

    DECLARE @returnvalue INT

    EXEC @returnvalue = [MYLINKEDSERVER].[mydb].[dbo].sp_update @no

    IF @returnvalue = 1
    BEGIN
        COMMIT
    END
    ELSE 
    BEGIN
        ROLLBACK
    END
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK
END CATCH

存储过程:

CREATE PROCEDURE [dbo].[sp_update] 
     @no NVARCHAR(20)
AS
BEGIN
    SET NOCOUNT ON

    BEGIN TRY
        BEGIN TRANSACTION
            DELETE FROM mytb 
            WHERE no = @no

            COMMIT
            RETURN 1
    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0
            ROLLBACK
        RETURN 0
    END CATCH
END

希望可以帮助某人

答案 1 :(得分:0)

尝试在要进行插入操作的服务器上执行以下命令。

EXEC sp_serveroption @server = 'ReadServerName',@optname = 'remote proc transaction promotion', @optvalue = 'false' ;

也尝试执行以下代码。我添加了BEGIN DISTRIBUTED TRANSACTION而不是BEGIN TRANSACTION

BEGIN TRY
    BEGIN DISTRIBUTED TRANSACTION
    INSERT INTO [Dev].[dbo].tb_test (no)  SELECT no from [MYLINKEDSERVER].[mydb].[dbo].tb_test 
    DELETE FROM [MYLINKEDSERVER].[mydb].[dbo].tb_test
    COMMIT
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK
END CATCH

有关详细信息,请访问MS site

答案 2 :(得分:0)

尝试一下,它应该起作用

DECLARE @ServerName SYSNAME
, @Message nvarchar(1000)
, @CMD1 nvarchar(max)
--
DECLARE @Server_List Table
( SrvID SMALLINT
, SrvName SYSNAME )
--
Set NoCount ON
--
-- Load up linked server list
--
BEGIN
INSERT INTO @Server_List (SrvID, SrvName)
SELECT SrvID
, SrvName 
FROM [master].[SYS].sysservers
ORDER BY SrvID ASC
END
--
SELECT TOP 1 @ServerName = SrvName
FROM @Server_List
ORDER BY SrvID ASC
--
-- Loop through the Linked Server List
--
WHILE EXISTS ( SELECT * FROM @Server_List )
BEGIN
SELECT @Message = 'Server Name is '+ @ServerName
--
RAISERROR (@Message, 10,1) WITH NOWAIT
--
SET @CMD1 = 'EXEC master.dbo.sp_serveroption @server=N'''
+ @ServerName
+ ''', @optname=N''rpc'', @optvalue=N''true'''
Exec sp_executesql @cmd1
--
SET @CMD1 = 'EXEC master.dbo.sp_serveroption @server=N'''
+ @ServerName
+ ''', @optname=N''rpc out'', @optvalue=N''true'''
Exec sp_executesql @cmd1
--
set @cmd1 = 'EXEC master.dbo.sp_serveroption @server = '''
+ @ServerName
+ ''', @optname=N''remote proc transaction promotion'', @optvalue=N''false'''
Exec sp_executesql @stmt=@cmd1,@params=N''
--
DELETE FROM @Server_List WHERE SrvName = @ServerName
--
SELECT TOP 1 @ServerName = SrvName
FROM @Server_List
ORDER BY SrvID ASC
--
END