SQLServer存储过程中事务的使用方法
网络编程 2021-07-05 13:43www.168986.cn编程入门
这篇文章主要介绍了SQLServer存储过程中事务的使用方法,简短的代码带大家更好的学习使用SQLServer存储过程中事务,感兴趣的小伙伴们可以参考一下
本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下
create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, @StockUnit varchar(50), @StockDate datetime, @TotalMoney money , @ActMoney money , @baseId int, @Description nvarchar(255) as declare @error int =0 --事务中操作的错误记录 --开启事务 begin transaction --实现进货信息的添加 insert into StockInfo values(@GoodsId, @Number, @StockPrice, @SupplierId, @EmpId, @StockUnit, @StockDate, @TotalMoney, @ActMoney,DEFAULT,@Description, @baseId) set @error+=@@ERROR --记录有可能产生的错误号 --获取当前进货信息的标识列 --判断当前商品有没有进货记录 if exists (select * from dbo.InventoryInfo where goodid=@GoodsId) --说明记录存在,直接修改库存数量 begin update dbo.InventoryInfo set GNumber=GNumber+@Number,TotalMoney+=@TotalMoney where goodid=@GoodsId set @error+=@@ERROR --记录有可能产生的错误号 end else --这个商品从来没有过进货记录,那么就应该添加新的存在信息 begin declare @GWarningNum int --此商品的预警数量 --获取预警数量 set @GWarningNum=(select WaringNum from dbo.GoodsInfo where GId=@GoodsId) insert into dbo.InventoryInfo values(@GoodsId,@Number,@baseId,@GWarningNum,@TotalMoney,'第一次进货',default) set @error+=@@ERROR --记录有可能产生的错误号 end --判断事务的提交或者回滚 if(@error<>0) begin rollback transaction return -1 --设置操作结果错误标识 end else begin mit transaction return 1 --操作成功的标识 end go
希望本文所述对大家学习数据库操作有所帮助。
上一篇:sqlserver锁表、解锁、查看销表的方法
下一篇:SqlLoader怎么使用
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程