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
  • 相关阅读:
    tnagios
    python-gearman使用
    yaml
    中国大陆互联网国际出口情况(2015年)
    vsftpd配置
    spoj-ASSIGN-bitDP
    spoj-ANARC05H -dp
    Light oj 1379 -- 最短路
    SPOJ-394-ACODE
    2018年东北农业大学春季校赛
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10811948.html
Copyright © 2011-2022 走看看