博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQLServer存储过程中事务的使用
阅读量:4356 次
发布时间:2019-06-07

本文共 1652 字,大约阅读时间需要 5 分钟。

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

 

转载于:https://www.cnblogs.com/ICE_Inspire/p/4513140.html

你可能感兴趣的文章
Principal Component Analysis 主元分析
查看>>
linux分割字符串操作
查看>>
PHP学习2
查看>>
多实例Mysql配置
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>
hdu4348区间更新的主席树+标记永久化
查看>>
ZOJ 2532 Internship
查看>>
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>
图的遍历 | 1013 连通块块数
查看>>
Kinect 开发 —— 进阶指引(上)
查看>>
python学习笔记(六)time、datetime、hashlib模块
查看>>
uva489(需要考虑周全)
查看>>
C-关键字(二)
查看>>
排序笔记
查看>>
下载360doc.com里的文章
查看>>
【转】globk和glorg中使用的apr文件
查看>>
导航,头部,CSS基础
查看>>
PostMessage 解析
查看>>