SQLServer存儲過程中事務的使用方法
作者:iceKnight 字體:[ ] 類型:轉載 時間:2015-12-13
這篇文章主要介紹了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) --說明記錄存在,直接修改庫存數量beginupdate dbo.InventoryInfo set GNumber=GNumber+@Number,TotalMoney+=@TotalMoney where goodid=@GoodsIdset @error+=@@ERROR --記錄有可能產生的錯誤號endelse --這個商品從來沒有過進貨記錄,那么就應該添加新的存在信息begindeclare @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) beginrollback transactionreturn -1 --設置操作結果錯誤標識 endelse begincommit transactionreturn 1 --操作成功的標識 endgo
希望本文所述對大家學習數據庫操作有所幫助。
您可能感興趣的文章:
Tags:sqlserver 事務
相關文章
大家感興趣的內容
最近更新的內容
常用在線小工具
新聞熱點
疑難解答