星空网 > 软件开发 > 数据库

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

 




原标题:SQLServer存储过程中事务的使用

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流