自己测试用的_存储过程中 SET XACT_ABORT 各种用法及显示结果 回滚

-- use  accmain

 

 

-- 删除表和过程
/**
 drop table gk_testa
 drop table gk_testb
 drop procedure sql_pro
**/
-- 创建表
/**
CREATE TABLE  gk_testa(
   id int identity(1,1) PRIMARY KEY,
   username Varchar(200),
   password  Varchar(200)
)

CREATE TABLE  gk_testb(
   id int identity(1,1) PRIMARY KEY,
   username Varchar(200),
   password  int
)
**/

--  删除数据
/**
 delete  from gk_testa
 delete  from gk_testb
**/

-- 1、  总体作为一个事务,整体提交或整体回滚,格式为:
 create procedure sql_pro AS
 SET XACT_ABORT ON
 begin TRAN
 insert into gk_testa(username,password)values('jack','123qwe');
 insert into gk_testb(username,password)values('jack','123qwe');
 COMMIT TRAN

-- 2、 每个语句作为一个事务,事务在错误行终止,错误行回滚,错误行之前的不回滚,格式为:
 create procedure sql_pro AS
 SET XACT_ABORT ON
 begin
 insert into gk_testa(username,password)values('jack','123qwe');
 insert into gk_testb(username,password)values('jack','123qwe');
 end

-- 3、带参数的存储过程(总体作为一个事务,整体提交或整体回滚),格式为:
 create procedure sql_pro (@usernamea Varchar(200), @passworda Varchar(200),@usernameb Varchar(200), @passwordb int)
 AS
 SET XACT_ABORT ON
 begin TRAN
 insert into gk_testa(username,password)values(@usernamea,@passworda);
 insert into gk_testb(username,password)values(@usernameb,@passwordb);
 COMMIT TRAN

--   执行过程(无参数)
exec  sql_pro;
--   执行过程(有参数)
exec  sql_pro 'aname','apassword','bname',222;


--   查询数据
 select * from gk_testa
 select * from gk_testb

 

 

 

================== 上面是sql Server 数据库测写法。

 

 

===================下面是参考的例子(各种数据库应该都支持吧)。

 

 


SET XACT_ABORT各种用法及显示结果
默认行为

  默认为SET XACT_ABORT OFF,没有事务行为。
SET XACT_ABORT ON

  SET XACT_ABORT ON分为两种:

  1、总体作为一个事务,整体提交或整体回滚,格式为:

SET XACT_ABORT ON
BEGIN TRAN
    --要执行的语句
COMMIT TRAN
GO

  2、每个语句作为一个事务,事务在错误行终止,错误行回滚,错误行之前的不回滚,格式为:

SET XACT_ABORT ON
BEGIN
    --要执行的语句
END
GO

测试
复制代码

--创建测试表
use MyDB
CREATE TABLE student
(   
        stuid int NOT NULL PRIMARY KEY,
        stuname varchar(50)
)
CREATE TABLE score
(
        stuid int NOT NULL REFERENCES student(stuid),
        score int
)
GO

--插入测试数据
INSERT INTO student VALUES (101,'zhangsan')
INSERT INTO student VALUES (102,'wangwu')
INSERT INTO student VALUES (103,'lishi')
INSERT INTO student VALUES (104,'maliu')
GO

---------------测试事务提交------------------
use MyDB
--只回滚错误行,语句还继续执行
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
/*
stuid       score
----------- -----------
101         90
102         78
103         81
104         65

(4 row(s) affected)
*/

use MyDB
--事务终止并全部回滚
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
/*
stuid       score
----------- -----------
(0 row(s) affected)
*/

use MyDB
--事务在错误行终止,错误行回滚,错误行之前的不回滚
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
/*
stuid       score
----------- -----------
101         90
102         78
(2 row(s) affected)
*/

 

 

 

 

 

来源:http://www.cnblogs.com/rob0121/articles/2320932.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值