1、回滚语句1: SET XACT_ABORT OFF BEGIN TRAN INSERT INTO score VALUES (101,90)INSERT INTO score VALUES (102,78)INSERT INTO score VALUES (107,76) /**//* Foreign Key Error */INSERT INTO score VALUES (103,81)INSERT INTO score VALUES (104
1、回滚语句1:
SET XACT_ABORT OFF
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107,76) /**//* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
go
结果1:只回滚错误行,语句还继续执行。
–select * from score
101 90
102 78
103 81
104 65
2、回滚语句2:
SET XACT_ABORT on
BEGIN TRAN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107,76) /**//* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
COMMIT TRAN
go
结果2:事务终止并全部回滚,结果为空。
–select * from score
3、回滚语句3:
事务在错误行终止,错误行回滚,错误行之前的不回滚
SET XACT_ABORT on
BEGIN
INSERT INTO score VALUES (101,90)
INSERT INTO score VALUES (102,78)
INSERT INTO score VALUES (107,76) /**//* Foreign Key Error */
INSERT INTO score VALUES (103,81)
INSERT INTO score VALUES (104,65)
END
go
结果3:出现这种是因为系统把每个insert语句都看成是单独的事务,所以错误行以前的是不回滚的。
–select * from score
101 90
102 78
到此这篇关于SQL SERVER提交事务回滚机制的文章就介绍到这了,更多相关SQL SERVER事务回滚内容请搜索每日运维以前的文章或继续浏览下面的相关文章希望大家以后多多支持每日运维!