触发每个插入行的触发以插入远程表

时间:2015-09-09 13:50:37

标签: sql-server sql-server-2008 stored-procedures triggers

我有两个不同的SQL 2008服务器,我没有权限在其中任何一个中创建链接服务器。 我在server1.table1上创建了一个触发器,使用OPENROWSET将相同的记录插入远程服务器server2.table1

我创建了一个存储过程来插入这条记录,我执行存储过程时没有问题。它将recored插入远程服务器。

问题是当我从触发器调用存储过程时,我收到一条错误消息。

任何人都可以帮我解决这个问题

存储过程:

USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

alter PROCEDURE [dbo].[InsertIntoRemoteTable]
        @Description nvarchar(50)
        AS

        insert into 
                    OPENROWSET(
                    'SQLNCLI', 'Server=Server2;UID=MySRV2user;PWD=MySRV2Password',
                    'SELECT Description FROM [RemoteDB].[dbo].[Table_1]')
        SELECT @Description 

触发:

    ALTER TRIGGER [dbo].[InsertTable1]
   ON  [DB1].[dbo].[Table_1] 
   for insert
AS 

BEGIN
    SET NOCOUNT ON;
    DECLARE @Desc nvarchar(50)
    Select @Desc = i.Descr from INSERTED i;
    EXEC   InsertIntoRemoteTable @Desc
END

当我尝试在表中插入新的描述值时,我收到以下错误消息:

"No row was updated
the data in row 1 was not committed
Error source .Net SqlClient Data provider.
Error Message: the operation could not be performed because OLE DB
provider "SQLNCLI10" for linked server "(null)" returned message "The partner transaction manager has disabled its support for remote/network transaction.".

correct the errors entry or press ESC to cancel the change(s).

任何人都可以帮忙 感谢

1 个答案:

答案 0 :(得分:0)

我认为你已经过度复杂了。这里实际上不需要存储过程。您可以使用简单的insert语句直接在触发器中插入另一个数据库。这消除了大量的复杂性,并且是基于它设置的。

ALTER TRIGGER [dbo].[InsertTable1]
   ON  [DB1].[dbo].[Table_1] 
   for insert
AS 

BEGIN
    SET NOCOUNT ON;

    INSERT INTO RemoteDB].[dbo].[Table_1](Description)
    Select i.Descr 
    from INSERTED i;
END