您可能没有在 tsqlt 测试中使用 TRY/CATCH 块。
错误信息“ There was also a ROLLBACK ERROR --> The current transaction cannot becommited and cannot be roll back to a savepoint. Roll back the entire transaction. ”可以重现如下:
1)创建一个引发错误并回滚的触发器,如下所示:
CREATE TRIGGER [dbo].[MyTable_IUDTR] ON [dbo].[MyTable]
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
BEGIN TRY
RAISERROR('MyError', 16, 1)
END TRY
BEGIN CATCH
THROW;
END CATCH
END
GO
2)创建一个tsqlt测试来检查返回的错误信息:
CREATE PROC [ut_MyTable_IUDTR].[test that the error is returned]
AS
BEGIN
DECLARE @ErrorMsg VARCHAR(50)
EXEC tsqlt.FakeTable @TableName = 'MyTable'
EXEC tsqlt.ApplyTrigger 'MyTable', 'MyTable_IUDTR'
INSERT INTO dbo.MyTable
( FirstName, LastName )
VALUES ( N'John',N'Smith')
SET @ErrorMsg = ERROR_MESSAGE()
EXEC tSQLt.AssertEqualsString @Expected = 'MyError', @Actual = @ErrorMsg
END
GO
3)运行测试:
EXEC [tSQLt].Run 'ut_MyTable_IUDTR.test that the error is returned'
4)您收到以下错误:
There was also a ROLLBACK ERROR --> The current transaction cannot be committed and cannot be rolled back to a savepoint. Roll back the entire transaction.
修复:
更改 tsqlt 测试以包括 TRY/CATCH 块,如下所示:
ALTER PROC [ut_MyTable_IUDTR].[test that the error is returned]
AS
BEGIN
DECLARE @ErrorMsg VARCHAR(50)
EXEC tsqlt.FakeTable @TableName = 'MyTable'
EXEC tsqlt.ApplyTrigger 'MyTable', 'MyTable_IUDTR'
BEGIN TRY
INSERT INTO dbo.MyTable
( FirstName, LastName )
VALUES ( N'John',N'Smith')
END TRY
BEGIN CATCH
SET @ErrorMsg = ERROR_MESSAGE()
END CATCH
EXEC tSQLt.AssertEqualsString @Expected = 'MyError', @Actual = @ErrorMsg
END
GO