save tran tranName

begin tran 语句将 @@Trancount加 1。Rollback tran将 @@Trancount递减到 0,但 Rollback tran savepoint_name 除外,它不影响 @@Trancount。Commit tran 或 Commit work 将 @@Trancount 递减 1

 

在SQL Server中使用rollback会回滚所有的未提交事务状态,但是有些时候我们只需要回滚部分语句,把不需要回滚的语句提到事务外面来,虽然是个方法,但是却破坏了事务的ACID。

其实我们可以使用SQL Server中的Savepoints来解决上述问题。

示例如下:

1.先建立测试表: 
CREATE TABLE [dbo].[ttt]( 
    [Id] [int] NULL, 
    [mark] [int] NULL 
)

 

2.SQL 语句 
begin tran 
    insert into ttt values(3,'3'); 
save tran point1 
    insert into ttt values(4,'4');

rollback tran point1

 

commit

 

执行结果如下: 
Id    mark 
3    3

可见,虽然3,4都在一个事务中,但是由于使用了SavePoints,所以3被提交了,4被回滚了。

posted on 2017-12-28 18:17  卖肾割阑尾  阅读(180)  评论(0编辑  收藏  举报

导航