欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQLServer--事务的创建

程序员文章站 2022-05-09 15:58:45
...

基本框架

SQLServer--事务的创建

use StudentManager
go
--事务基本框架
declare @errorSum int --定义变量,用于累计事务执行过程中的错误
set @errorSum =0 --初始化为0,即无错误
begin transaction
    begin
        if(@errorSum>0)
            rollback transaction
        else
            commit transaction  --提交回滚事务


    end

go



事务的使用

A账户转到B账户,但A账户设置了约束余额不少于1元

SQLServer--事务的创建

错误依然存在:

SQLServer--事务的创建

但结果依然正确:

SQLServer--事务的创建

可以清楚的看到发生错误,但是由于事务回滚,对最后的结果没有任何影响。


use StudentManager
go

--事务基本框架
declare @errorSum int --定义变量,用于累计事务执行过程中的错误
set @errorSum =0 --初始化为0,即无错误
begin transaction
    begin
        update CardAccount set CurrentMoney=CurrentMoney-1000
        where StudentId=100001
        set @aaa@qq.comaaa@qq.com@ERROR

        update CardAccount set CurrentMoney=CurrentMoney+1000
        where StudentId = 100002
        set @errorSum = @aaa@qq.com@ERROR
        if(@errorSum>0)
            rollback transaction
        else
            commit transaction  --提交回滚事务
    end

go

select Students.StudentId,StudentName,CurrentMoney from Students
inner join CardAccount on Students.StudentId=CardAccount.StudentId


--update CardAccount set CurrentMoney=CurrentMoney+900
--      where StudentId=100001
--select * from CardAccount
相关标签: 事务