在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被回滚了。