zoukankan      html  css  js  c++  java
  • SQLServer存储过程中事务的使用

     1 create proc usp_Stock
     2 @GoodsId int, 
     3 @Number int, 
     4 @StockPrice money, 
     5 @SupplierId int, 
     6 @EmpId int, 
     7 @StockUnit varchar(50), 
     8 @StockDate datetime, 
     9 @TotalMoney money , 
    10 @ActMoney money , 
    11 @baseId int,
    12 @Description nvarchar(255)
    13 as
    14     declare @error int =0 --事务中操作的错误记录
    15     --开启事务
    16     begin transaction
    17         --实现进货信息的添加
    18         insert into StockInfo values(@GoodsId, @Number, @StockPrice, @SupplierId, @EmpId, @StockUnit, @StockDate, @TotalMoney, @ActMoney,DEFAULT,@Description, @baseId)
    19         set @error+=@@ERROR --记录有可能产生的错误号    
    20         --获取当前进货信息的标识列
    21         --判断当前商品有没有进货记录
    22         if exists (select * from dbo.InventoryInfo where goodid=@GoodsId) --说明记录存在,直接修改库存数量
    23             begin
    24                 update  dbo.InventoryInfo set GNumber=GNumber+@Number,TotalMoney+=@TotalMoney where goodid=@GoodsId
    25                 set @error+=@@ERROR --记录有可能产生的错误号            
    26         end    
    27         else --这个商品从来没有过进货记录,那么就应该添加新的存在信息
    28             begin
    29                 declare @GWarningNum int  --此商品的预警数量
    30                 --获取预警数量
    31                 set @GWarningNum=(select WaringNum from dbo.GoodsInfo where GId=@GoodsId)
    32                 insert into     dbo.InventoryInfo values(@GoodsId,@Number,@baseId,@GWarningNum,@TotalMoney,'第一次进货',default)
    33                 set @error+=@@ERROR --记录有可能产生的错误号            
    34             end
    35 --判断事务的提交或者回滚
    36 if(@error<>0)
    37     begin
    38         rollback transaction
    39         return -1 --设置操作结果错误标识
    40     end
    41 else
    42     begin
    43         commit transaction
    44         return 1 --操作成功的标识
    45     end
    46 go
    欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果感觉对您有用,请点击推荐。您的支持,是我的动力!
  • 相关阅读:
    英语apyrite红碧玺apyrite单词
    英语SouthRedAgate南红玛瑙
    英语kutnahorite金田黄kutnahorite单词
    英语chalchite蓝绿松石chalchite单词
    单词demantoite翠榴石demantoite英语
    英语fieldyellowstone田黄石fieldyellowstone单词
    Http通讯Util
    redis分布式锁工具类
    永不重复的id生成器
    二维码生成工具类
  • 原文地址:https://www.cnblogs.com/ICE_Inspire/p/4513140.html
Copyright © 2011-2022 走看看