i'm working with Linked Server, and this code working fine
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
but when i use it with COMMIT TRANSACTION, like this
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
show error
OLE DB provider "SQLNCLI11" for linked server "MYLINKEDSERVER" returned message "The partner transaction manager has disabled its support for remote/network transactions.".
What is wrong?
The solution is to write a stored procedure on remote server with return value, and execute it locally:
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
The stored procedure:
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
Hope help someone
Try executing below command on Server on which you are going to do insert operation.
EXEC sp_serveroption @server = 'ReadServerName',@optname = 'remote proc transaction promotion', @optvalue = 'false' ;
Also try executing below code. I have added BEGIN DISTRIBUTED TRANSACTION
instead of 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
Details are available at MS site .
Try this it should work
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.