ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

SQL 事务(Transaction)完整版学习笔记(含转账案例+存储过程+异常捕获)

SQL 事务(Transaction)完整版学习笔记(含转账案例+存储过程+异常捕获) 一、事务核心概念必考简答题1. 事务定义事务(TRANSACTION)是作为单个逻辑工作单元执行的一系列操作。多个操作作为一个整体向系统提交要么全部执行成功要么全部不执行回滚事务是不可分割的工作逻辑单元。2. 事务四大特性ACID 必背原子性(Atomicity)事务整体不可拆分所有操作同时成功或同时失败回滚无部分执行情况。一致性(Consistency)事务执行前后数据库数据始终保持合法、一致的状态不会出现数据错乱。隔离性(Isolation)多个并发事务相互独立、互不干扰彼此隔离运行。永久性(Durability)事务提交成功后对数据库的修改永久生效不会丢失。3. 事务三大分类1自动提交事务默认模式SQL Server 默认机制每条独立T-SQL语句都是一个单独事务执行成功自动提交失败自动回滚。2显式事务重点、最常用手动通过begin transaction开启事务手动控制commit提交、rollback回滚适用于多语句联动业务转账、增减库存等。3隐性事务开启开关SET IMPLICIT_TRANSACTIONS ON后后续所有T-SQL语句自动开启事务需手动提交/回滚无需手动写开启语句。二、无事务的业务BUG反面案例1. 创建银行卡表测试数据-- 创建银行卡资金表带余额校验余额必须大于1规范字段类型 create table CardNew ( StudentId int identity(1000,1) primary key not null, StudentName varchar(10) not null, CurrentMoney money check(CurrentMoney 1) ) -- 初始化测试数据 insert into CardNew(StudentName,CurrentMoney) values(赵云,1400) insert into CardNew(StudentName,CurrentMoney) values(刘备,1500) insert into CardNew(StudentName,CurrentMoney) values(关羽,1600) insert into CardNew(StudentName,CurrentMoney) values(曹操,1700) -- 【BUG场景】无事务转账典型数据错乱问题 -- 1001刘备余额1500转出1000后剩余500符合check约束可执行 update CardNew set CurrentMoneyCurrentMoney-1000 where StudentId1001 -- 1002关羽转入1000 update CardNew set CurrentMoneyCurrentMoney1000 where StudentId1002 -- 查看数据 select * from CardNew -- 【手动制造报错场景】重新初始化数据演示BUG truncate table CardNew insert into CardNew(StudentName,CurrentMoney) values(赵云,1400) insert into CardNew(StudentName,CurrentMoney) values(刘备,1500) insert into CardNew(StudentName,CurrentMoney) values(关羽,1600) insert into CardNew(StudentName,CurrentMoney) values(曹操,1700) -- 错误场景1001转出1500余额0违反 CurrentMoney1 约束 update CardNew set CurrentMoneyCurrentMoney-1500 where StudentId1001 -- 本条正常执行导致转出失败、转入成功数据不一致 update CardNew set CurrentMoneyCurrentMoney1500 where StudentId1002 select * from CardNewBUG原因默认自动提交事务两条update语句相互独立前一句报错回滚后一句正常提交导致数据不一致。三、基础显式事务ERROR 错误捕获通过系统全局变量ERROR累计错误数判断是否回滚/提交事务解决转账数据错乱问题。-- 重置测试数据 truncate table CardNew insert into CardNew(StudentName,CurrentMoney) values(赵云,1400) insert into CardNew(StudentName,CurrentMoney) values(刘备,1500) insert into CardNew(StudentName,CurrentMoney) values(关羽,1600) insert into CardNew(StudentName,CurrentMoney) values(曹操,1700) declare errorNum int -- 定义错误计数器 set errorNum0 begin transaction -- 手动开启显式事务 -- 转出操作余额清零触发约束报错 update CardNew set CurrentMoneyCurrentMoney-1500 where StudentId1001 set errorNumerrorNumERROR -- 累加SQL错误码 -- 转入操作 update CardNew set CurrentMoneyCurrentMoney1500 where StudentId1002 set errorNumerrorNumERROR -- 事务提交/回滚判断 if(errorNum 0) begin rollback transaction -- 任意步骤报错整体回滚 print 事务执行失败已全部回滚 end else begin commit transaction -- 无错误正常提交 print 事务执行成功数据已更新 end select * from CardNew四、基础转账事务存储过程可直接调用封装通用转账逻辑支持自定义入账账号、出账账号、转账金额适配所有转账场景。-- 存在则删除存储过程 if exists(select * from sysobjects where nameTest1) drop proc Test1 go -- 基础通用转账事务存储过程 create proc Test1 inAccount int, -- 入账账号 outAccount int, -- 出账账号 jine int -- 转账金额 as declare errorNum int set errorNum0 begin transaction -- 开启事务 -- 出账账号扣款 update CardNew set CurrentMoneyCurrentMoney-jine where StudentIdoutAccount set errorNumerrorNumERROR -- 入账账号加款 update CardNew set CurrentMoneyCurrentMoneyjine where StudentIdinAccount set errorNumerrorNumERROR -- 事务逻辑判断 if(errorNum 0) begin rollback transaction print 转账失败事务回滚 end else begin commit transaction print 转账成功事务提交 end go -- 测试1正常转账成功 truncate table CardNew insert into CardNew(StudentName,CurrentMoney) values(赵云,1400) insert into CardNew(StudentName,CurrentMoney) values(刘备,1500) insert into CardNew(StudentName,CurrentMoney) values(关羽,1600) insert into CardNew(StudentName,CurrentMoney) values(曹操,1700) exec Test1 1003,1000,300 select * from CardNew -- 测试2异常转账触发约束失败回滚 exec Test1 1003,1000,1200 select * from CardNew五、高级异常捕获TRY-CATCH 语法1. 基础TRY-CATCH异常捕获用于捕获代码运行异常避免程序中断可手动抛出错误、终止执行。-- 基础 TRY-CATCH 异常捕获语法 begin try -- 强制类型转换错误模拟业务异常 declare i int set i CAST(abc as int) print i end try begin catch -- 抛出自定义错误信息 raiserror(字符串无法转换为数字数据类型错误,16,1) print 代码执行异常已捕获错误 end catch2. TRY-CATCH 结合事务-- TRY-CATCH 事务 标准组合用法 begin tran begin try delete from StudentInfo where StudentId1000 -- 主动抛出异常测试回滚效果 raiserror(无此学生编号删除失败,16,1) commit tran -- 无异常则提交事务 print 执行成功事务已提交 end try begin catch print 代码执行出错事务已回滚 if(TRANCOUNT 0) rollback tran -- 存在未提交事务则回滚 end catch核心逻辑TRY包裹正常业务代码CATCH捕获所有异常只要出错立刻回滚事务保证数据一致。六、终极完整版转账存储过程双重校验业务判断双异常捕获整合业务逻辑校验 ERROR错误捕获 TRY-CATCH异常捕获企业级标准写法无漏洞、安全性最高。-- 终极完整版业务校验事务双重异常捕获 转账存储过程 if exists ( select * from sysobjects where nametest01) drop proc test01 go create proc test01 inAccount int, -- 入账账号 outAccount int, -- 出账账号 jine int -- 转账金额 as declare errorNum int set errorNum0 -- 第一层前置业务合法性校验拦截逻辑错误 -- 1. 转账金额不能为负数 if (jine 0) begin raiserror(转账金额不合法必须大于0,16,1) return end -- 2. 校验转出账号是否存在 if not exists (select 1 from CardNew where StudentIdoutAccount) begin raiserror(转出账户不存在,16,1) return end -- 3. 校验转入账号是否存在 if not exists (select 1 from CardNew where StudentIdinAccount) begin raiserror(转入账户不存在,16,1) return end -- 4. 校验转出账户余额是否充足 declare yue money select yue CurrentMoney from CardNew where StudentIdoutAccount if(yue jine) begin raiserror(账户余额不足无法转账,16,1) return end -- 第二层事务 双重错误捕获拦截系统SQL错误 begin tran -- 开启事务 begin try -- 转出扣款 update CardNew set CurrentMoneyCurrentMoney-jine where StudentIdoutAccount set errorNumerrorNumERROR -- 转入加款 update CardNew set CurrentMoneyCurrentMoneyjine where StudentIdinAccount set errorNumerrorNumERROR -- 捕获轻微SQL执行错误 if(errorNum 0) begin raiserror(SQL语句执行出错事务回滚,16,1) rollback transaction end else begin commit transaction print 转账业务执行成功 end end try begin catch -- 捕获严重异常防止事务悬挂 if(TRANCOUNT 0) begin rollback tran print 程序异常事务强制回滚 end return end catch go -- 测试调用 truncate table CardNew insert into CardNew(StudentName,CurrentMoney) values(赵云,1400) insert into CardNew(StudentName,CurrentMoney) values(刘备,1500) insert into CardNew(StudentName,CurrentMoney) values(关羽,1600) insert into CardNew(StudentName,CurrentMoney) values(曹操,1700) -- 正常转账 exec test01 1001,1000,200 select * from CardNew -- 异常测试余额不足 exec test01 1002,1000,2000 select * from CardNew七、核心考点总结考前必背事务作用保证多步关联操作数据一致性防止部分执行、数据错乱转账核心场景。ACID四大特性原子性、一致性、隔离性、永久性简答题必考。事务三语句begin transaction开启、commit提交、rollback回滚。错误捕获区别ERROR捕获单条SQL错误TRY-CATCH捕获所有运行异常严重错误。双层校验逻辑先业务人工校验金额、账号、余额再系统事务校验零漏洞。TRANCOUNT判断当前是否存在未提交的事务避免重复回滚报错。
返回列表