当前位置: 代码迷 >> SQL >> SQLServer存储过程中事宜的使用
  详细解决方案

SQLServer存储过程中事宜的使用

热度:65   发布时间:2016-05-05 10:24:44.0
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 as14     declare @error int =0 --事务中操作的错误记录15     --开启事务16     begin transaction17         --实现进货信息的添加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             begin24                 update  dbo.InventoryInfo set GNumber=GNumber+@Number,TotalMoney+=@TotalMoney where goodid=@GoodsId25                 set @error+=@@ERROR --记录有可能产生的错误号            26         end    27         else --这个商品从来没有过进货记录,那么就应该添加新的存在信息28             begin29                 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             end35 --判断事务的提交或者回滚36 if(@error<>0)37     begin38         rollback transaction39         return -1 --设置操作结果错误标识40     end41 else42     begin43         commit transaction44         return 1 --操作成功的标识45     end46 go

 

  相关解决方案