zoukankan      html  css  js  c++  java
  • sql 事务

    if exists(select * from sysobjects where  name='bank'  )
      drop table bank
      --创建银行表
      create table bank
    (
        customerName char(10),    --顾客姓名
        currentMoney money        --当前余额
    )
    go
    /**//*--添加约束,帐户不能少于元--*/
    alter table bank add
            constraint CK_currentMoney check(currentMoney>=1)
    /**//*--插入测试数据--*/
    /**//*--插入测试数据--*/
    insert into bank(customerName,currentMoney)
    select '张三',1000 union
    select '李四',1

    select * from bank
    go

    /**//*--使用事务--*/
    go
    --恢复原来的数据
    --update bank set currentMoney=currentMoney-1000 where customerName='李'
    set nocount on    --不显示受影响的行数
    print '查看转帐事务前的余额'
    select * from bank
    go

    /**//*--开始事务--*/
    begin transaction
    declare @errorSum int    --定义变量,用于累计事务执行过程中的错误
    /**//*--转帐--*/
     set @errorSum=0
    update bank set currentMoney=currentMoney+800 where customerName='李四'
    set @errorSum=@errorSum+@@error --累计是否有错误
    update bank set currentMoney=currentMoney-800 where customerName='张三'
    set @errorSum=@errorSum+@@error    --累计是否有错误


    print '查看转帐事务过程中的余额'
    select * from bank
    print @errorSum
    /**//*--根据是否有错误,确定事务是提交还是回滚--*/
    if @errorSum>0
        begin
            print '交易失败,回滚事务.'
            rollback transaction
        end
    else
        begin
            print '交易成功,提交事务,写入硬盘,永久保存!'
            commit transaction
        end
    go

    print '查看转帐后的余额'
    select * from bank
    go

     


  • 相关阅读:
    用python爬虫爬取去哪儿4500个热门景点,看看国庆不能去哪儿
    北邮-TCP/IP
    python中遇到的小坑记录
    pip将依赖包导成requirements.txt文件
    关于requsts请求json格式单双引号导致json解析失败的问题
    python中,ddt模块的使用
    openpxyl模块完成excel数据读写的常用操作
    关于pip下载源的问题
    集合
    关于Jmeter变量嵌套
  • 原文地址:https://www.cnblogs.com/dengxixi/p/8086763.html
Copyright © 2011-2022 走看看