存储过程中的 SET XACT_ABORT ON 和事务
在存储过程中写SET XACT_ABORT ON 有什么用?
SET XACT_ABORT ON是设置事务回滚的!
当为ON时,如果你存储中的某个地方出了问题,整个事务中的语句都会回滚
为OFF时,只回滚错误的地方
第一种情况:每次成功执行一条语句就立刻进行提交事务 (注意commit tran的位置)
use sales --指定数据库
go alter table T_UserInfoTwo
add constraint ck_id check(id between 1 and 15) --给T_UserInfoTwo表的Id添加约束
go if exists(select * from sys.objects where name='proc_userinfotwo_insert')
drop proc proc_userinfotwo_insert --如果存在此存储过程则删除
go create proc proc_userinfotwo_insert --创建存储过程
as
begin
declare @id int
set @id=1
while @id<20
begin
begin try
begin tran --开启事务(设置反悔点)
insert into T_UserInfoTwo values(@id,'小雅',21,0,'','123@qq.com',' 湖南常德','朋友',1,'坏心眼女巫');
commit tran --提交事务(不反悔,将数据插入到表中)
end try
begin catch
rollback tran --抛出异常后回滚
end catch set @id =@id+1; --变量自增1
end
end
go

use sales --指定数据库
go alter table T_UserInfoTwo
add constraint ck_id check(id between 1 and 15) --给T_UserInfoTwo表的Id添加约束
go if exists(select * from sys.objects where name='proc_userinfotwo_insert')
drop proc proc_userinfotwo_insert --如果存在此存储过程则删除
go create proc proc_userinfotwo_insert --创建存储过程
as
begin
declare @id int
set @id=1
begin
begin try
begin tran --开启事务(设置反悔点)
while @id<20
begin
insert into T_UserInfoTwo values(@id,'小雅',21,0,'','123@qq.com',' 湖南常德','朋友',1,'坏心眼女巫');
set @id =@id+1; --变量自增1
end
commit tran --提交事务(不反悔,将数据插入到表中) 特别要注意这个commit tran的位置,不如果不想每执行完一条数据就提交事务,就应该讲这个commit tran放到while循环外面来。
end try
begin catch
begin
rollback tran --抛出异常后回滚
end
end catch
end
end
go

存储过程中的 SET XACT_ABORT ON 和事务的更多相关文章
- 存储过程中使用事务,sql server 事务,sql事务
一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 ...
- Sqlserver 存储过程中结合事务的代码
Sqlserver 存储过程中结合事务的代码 --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...
- 存储过程中使用事务与try catch
一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : ) ) ...
- 存储过程中使用事务和try catch
一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : Cre ...
- sql 在存储过程中使用事务(转)
本来想自己写一下,后来发现这个写的比我理解的要好,所以直接拽过来了,链接地址:https://www.cnblogs.com/RascallySnake/archive/2010/05/17/1737 ...
- ADOConnectoin事务和存储过程中的Begin tran commit
一直以来我都是在存储过程中使用事务 create proc usp_proc begin begin tran ..... commit end 那么我现在问一个问题,如果在BCB的代码中写这样的代 ...
- SQL中存储过程中使用事务,并且加入异常处理机制.
--存储过程中使用事务,并且加入异常处理机制. -- ============================================= CREATE PROCEDURE [dbo].[UP_ ...
- 资深架构师Sum的故事:(Mysql)InnoDB下,存储过程中事务的处理
| 故事背景 话说有一回,X市X公司的产品经理Douni兴致冲冲的跑来和Sum(Sum,X市X公司资历8年程序猿,技能:深思.熟虑.心细.深究.技术过敏.口头禅:嗯,容我想想.坚信:只要赚钱的业务,我 ...
- SQLServer存储过程中事务的使用
create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, ), ...
随机推荐
- 010_动态语言与鸭子类型及python2和3的区别
一. 动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, ...
- node.js如何引用其它js文件
以Java来说,比如要实现第三方存储,我可能需要导入对应的库,以maven为例,使用腾讯云或者七牛云.阿里云,我需要导入对应的maven依赖.再比如,有些时候我们封装某个类,而那个类不在该包下,我们需 ...
- Flask 框架 重定向,捕获异常,钩子方法及使用jsonify在网页返回json数据
Flask 框架中常用到重定向方法来实现路由的跳转 ,路由跳转又分为站内跳转和站外跳转 常用的站内跳转方法为url_for 而常用的站外跳转为redirect 在这里提示一下: 在使用两种方法是须调 ...
- PEP8 python规范神器
如需转载,请注明出处:小婷儿的博客:https://www.cnblogs.com/xxtalhr/p/10645992.html 一.Jupyter notebook 篇 Jupyter noteb ...
- WPF PasswordBox不支持绑定解决方法
原文:WPF PasswordBox不支持绑定解决方法 PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现.直接插入代码 public class Passw ...
- 【原创】Innodb中mysql如何快速删除2T的大表
小漫画 来,先来看小漫画陶冶一下情操 OK,这里就说了.假设,你有一个表erp,如果你直接进行下面的命令 drop table erp 这个时候所有的mysql的相关进程都会停止,直到drop结束,m ...
- 三、临时弹出一个QQ对话窗口
第一种:需要添加好友才可以访问 <a href="http://wpa.qq.com/msgrd?v=3&uin=317985559&site=qq&menu= ...
- H5 58-网页的布局方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- A direct formulation for sparse PCA using semidefinite programming
目录 背景 Sparse eigenvectors(单个向量的稀疏化) 初始问题(low-rank的思想?) 等价问题 最小化\(\lambda\) 得到下列问题(易推) 再来一个等价问题 条件放松( ...
- python学习第七篇——字典访问键与值
此程序的目的在于,正确而简单的访问字典的键与值 favorite_languages={ 'jen':['python','c'], 'sarah':['c'], 'edward':['ruby',' ...