设置触发器时无法进行SQL Azure数据同步

时间:2012-05-18 14:51:13

标签: azure-sql-database azure-data-sync

我尝试使用 SQL Azure Data Sync 在SQL-Azure-DB和内部部署的SQL-Server 2008R2之间进行同步。

我只需同步一个表,现在只有90行。 Azure端的表有一个触发器集,但是我本地sql-server上的表没有这个触发器(因为除了共享的一个表之外,这两个数据库没有任何共同点。)

当我尝试同步时失败并出现此错误:

Sync failed with the exception "GetStatus failed with exception:
Sync worker failed, checked by GetStatus method.

Failure details:
An unexpected error occurred when applying batch file C:\Resources\directory\61e9d741c80a47b4ae195f835e62fcda.NTierSyncServiceWorkerRole.LS1\DSS_syncjobmxd24sznwfq5idekfaopaery\a0e8b11a-a08c-4081-b929-e3f80b70f045.batch.
See the inner exception for more details.

Inner exception:
Failed to execute the command 'BulkInsertCommand' for table 'dbo.tblUser';
the transaction was rolled back. Ensure that the command syntax is correct.

Inner exception:
SqlException Error Code: -2146232060 - SqlError Number:512,
Message: Subquery returned more than 1 value.
This is not permitted when the subquery follows =, !=, <, <= , >, >=
or when the subquery is used as an expression.

SqlError Number:3621,
Message: The statement has been terminated. 
For more information, provide tracing id ‘bb8e3787-27c1-4b7e-9a26-6db2ff6724d3’ to customer support.

当我停用触发器时,同步有效!

我的触发器:

    CREATE TRIGGER [dbo].[UpdateUsersTable]
    ON [dbo].[tblUser]
    AFTER INSERT AS BEGIN SET NOCOUNT ON;

    INSERT INTO  [dbo].[Users] ([userID], [PartnerOrganizationId])
    VALUES ((select [userID] from inserted), (select [country] from inserted))
    END

作为一种方法,我想也许是因为我的触发器和同步触发器以错误的顺序启动,所以我尝试了:

    exec sp_settriggerorder @triggername = 'tblUser_dss_insert_trigger',
                        @order = 'first',
                        @stmttype = 'insert',
                        @namespace = null

    exec sp_settriggerorder @triggername = 'UpdateUsersTable',
                    @order = 'last',
                    @stmttype = 'insert',
                    @namespace = null
    go

但这并没有改变任何事情。仍然是同样的错误。

我是否有机会在启用触发器的情况下同步该表?

1 个答案:

答案 0 :(得分:1)

在您的触发器中,您正在使用INSERT导致此问题。

请为INSERT,UPDATE和DELETE创建单独的触发器。系统需要比较inserted和deleted表之间的行数,以确定触发触发器的操作。为INSERT / DELETE设置单独的触发器可以解决这个问题。

类似的讨论显示完全相同的问题here

更多信息

您的触发器有错误,并且对于任何多行插入都会失败。它应该是这样的:

CREATE TRIGGER [dbo].[UpdateUsersTable]
ON [dbo].[tblUser]
AFTER INSERT AS BEGIN SET NOCOUNT ON;

INSERT INTO  [dbo].[Users] ([userID], [PartnerOrganizationId])
 select [userID], [country] from inserted
END