zoukankan      html  css  js  c++  java
  • 案例银行转账存储过程

     1 if object_id(N'usp_transfer', N'P') is not null
     2     drop proc usp_transfer
     3 GO
     4 create proc usp_transfer
     5 @from char(4),    ----转账人    
     6 @to char(4),    --收账人    
     7 @balance money,    --转账金额
     8 @resultNumber int output    --转账结果(1表示成功,2表示失败,3表示余额不足)
     9 as
    10 begin
    11     --1.判断转账人余额是否足够
    12     declare @money money
    13     select @money = balance from bank where cid = @from
    14     if(@money - @balance >= 10)
    15     begin
    16         --开始转账
    17         begin transaction --开始事务
    18             declare @sum int = 0  --用于统计错误,初始值为0
    19             --2.转账人扣钱
    20             update bank set balance = balance - @balance where cid = @from
    21             set @sum = @sum + @@error   -- 如果有错误,则错误累加
    22             --3.收账人收钱
    23             update bank set balance = balance + @balance where cid = @to
    24             set @sum = @sum + @@error    --如果有错误,则错误累加
    25             --4.判断是否执行成功,进行提交或回滚
    26             if @sum <> 0
    27             begin
    28                 set @resultNumber = 2    --转账失败
    29                 rollback    --事务回滚
    30             end
    31             else
    32             begin
    33                 set @resultNumber = 1    --转账成功
    34                 commit    --事务提交
    35             end
    36     end
    37     else
    38     begin
    39         set @resultNumber = 3    --余额不足
    40     end
    41 end
    42 go

    执行

    1 declare @resultN int
    2 exec usp_transfer @from = '0001',@to = '0002',@balance = 900,@resultNumber = @resultN output
    3 print @resultN
  • 相关阅读:
    使用Windows Live Writer发布日志
    下雪
    Oracle中拼出树型结构
    [转载]Javascript中最常用的55个经典技巧
    博客访问者来自15个国家和地区
    [转载]一个帐号同一时间只能一个人登录
    换了博客的皮肤
    常见的开源软件许可
    java5中的Arrays
    青花瓷
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10811948.html
Copyright © 2011-2022 走看看