我尝试使用 SQL Azure Data Sync 在 SQL-Azure-DB 和本地 SQL-Server 2008R2 之间进行同步。
我只需要同步一个现在只有 90 行的表。Azure 端的表有一个触发器集,但我的本地 sql 服务器上的表没有这个触发器(因为除了共享一个表之外,这两个数据库没有任何共同点(。
当我尝试同步时,它失败并出现此错误:
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:Resourcesdirectory61e9d741c80a47b4ae195f835e62fcda.NTierSyncServiceWorkerRole.LS1DSS_syncjobmxd24sznwfq5idekfaopaerya0e8b11a-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
但这并没有改变任何事情。还是同样的错误。
我是否有机会在启用触发器的情况下同步该表?
在触发器中,您正在使用导致此问题的插入。
请为插入、更新和删除创建单独的触发器。系统确实需要比较插入和删除的表之间的行计数,以确定触发触发器的操作。为插入/删除使用单独的触发器应该可以解决此问题。
类似的讨论在这里显示了完全相同的问题
更多信息
触发器存在错误,对于任何多行插入都将失败。它应该是这样的:
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