嵌套事务无法回滚
Nested transaction can't be rolled back
我有 SQL Server 2008 并想进行这样的交易:
begin transaction oo;
......
begin try
save transaction xx;
alter table ....; -- this will fail
alter table ....;
alter table ....;
end try
begin catch
rollback transaction xx; -- error here
end catch;
......
commit transaction oo;
在 rollback transaction xx;
,我收到消息
3931 The current transaction cannot be committed and cannot be rolled back to a savepoint. Roll back the entire transaction.
我做错了什么?
更新 场景说明:
有一个大事务"oo",它会将数据库的table结构从产品版本X更改为产品版本Y。
在嵌套交易中,用户特定的-table应该尝试改变(=内部交易)。
如果特定于用户的 table 以某种方式损坏,则不应回滚整个产品升级过程。
另一方面,如果在主要产品 table 升级(外部事务)期间出现其他问题,则不应升级用户特定的 tables。
Reference
你必须在 CATCH
块中使用这一行
ROLLBACK TRANSACTION;
这将回滚所有事务,
当你在上面的声明中使用这个(在 Q 中发布)时,它会
给我们错误
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
为此,您必须将此行放在 TRY
块中
COMMIT TRANSACTION oo;
然后你终于这样说了
BEGIN TRANSACTION oo;
BEGIN TRY
SAVE TRANSACTION xx;
CREATE TABLE test (ID INT); -- this will fail from second time
SELECT 3;
COMMIT TRANSACTION oo;
END TRY
BEGIN catch
ROLLBACK TRANSACTION;
END CATCH;
更新 评论后
BEGIN TRY
BEGIN TRANSACTION xx1;
select 1; -- this will always success
COMMIT TRANSACTION xx1;
BEGIN TRANSACTION xx2;
CREATE TABLE test (id int); -- this will fail from second time
COMMIT TRANSACTION xx2;
BEGIN TRANSACTION xx3;
select 3; -- this will fail from second time
COMMIT TRANSACTION xx3;
END TRY
BEGIN catch
ROLLBACK TRANSACTION
END CATCH;
我有 SQL Server 2008 并想进行这样的交易:
begin transaction oo;
......
begin try
save transaction xx;
alter table ....; -- this will fail
alter table ....;
alter table ....;
end try
begin catch
rollback transaction xx; -- error here
end catch;
......
commit transaction oo;
在 rollback transaction xx;
,我收到消息
3931 The current transaction cannot be committed and cannot be rolled back to a savepoint. Roll back the entire transaction.
我做错了什么?
更新 场景说明:
有一个大事务"oo",它会将数据库的table结构从产品版本X更改为产品版本Y。
在嵌套交易中,用户特定的-table应该尝试改变(=内部交易)。
如果特定于用户的 table 以某种方式损坏,则不应回滚整个产品升级过程。
另一方面,如果在主要产品 table 升级(外部事务)期间出现其他问题,则不应升级用户特定的 tables。
Reference
你必须在 CATCH
块中使用这一行
ROLLBACK TRANSACTION;
这将回滚所有事务, 当你在上面的声明中使用这个(在 Q 中发布)时,它会 给我们错误
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
为此,您必须将此行放在 TRY
块中
COMMIT TRANSACTION oo;
然后你终于这样说了
BEGIN TRANSACTION oo;
BEGIN TRY
SAVE TRANSACTION xx;
CREATE TABLE test (ID INT); -- this will fail from second time
SELECT 3;
COMMIT TRANSACTION oo;
END TRY
BEGIN catch
ROLLBACK TRANSACTION;
END CATCH;
更新 评论后
BEGIN TRY
BEGIN TRANSACTION xx1;
select 1; -- this will always success
COMMIT TRANSACTION xx1;
BEGIN TRANSACTION xx2;
CREATE TABLE test (id int); -- this will fail from second time
COMMIT TRANSACTION xx2;
BEGIN TRANSACTION xx3;
select 3; -- this will fail from second time
COMMIT TRANSACTION xx3;
END TRY
BEGIN catch
ROLLBACK TRANSACTION
END CATCH;