SQLServer存儲過程中事務(wù)的使用方法
發(fā)布日期:2021-12-21 13:25 | 文章來源:源碼中國
本文為大家分享了SQLServer存儲過程中事務(wù)的使用方法,具體代碼如下
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 --事務(wù)中操作的錯誤記錄 --開啟事務(wù) begin transaction --實(shí)現(xiàn)進(jìn)貨信息的添加 insert into StockInfo values(@GoodsId, @Number, @StockPrice, @SupplierId, @EmpId, @StockUnit, @StockDate, @TotalMoney, @ActMoney,DEFAULT,@Description, @baseId) set @error+=@@ERROR --記錄有可能產(chǎn)生的錯誤號 --獲取當(dāng)前進(jìn)貨信息的標(biāo)識列 --判斷當(dāng)前商品有沒有進(jìn)貨記錄 if exists (select * from dbo.InventoryInfo where goodid=@GoodsId) --說明記錄存在,直接修改庫存數(shù)量 begin update dbo.InventoryInfo set GNumber=GNumber+@Number,TotalMoney+=@TotalMoney where goodid=@GoodsId set @error+=@@ERROR --記錄有可能產(chǎn)生的錯誤號 end else --這個商品從來沒有過進(jìn)貨記錄,那么就應(yīng)該添加新的存在信息 begin declare @GWarningNum int --此商品的預(yù)警數(shù)量 --獲取預(yù)警數(shù)量 set @GWarningNum=(select WaringNum from dbo.GoodsInfo where GId=@GoodsId) insert into dbo.InventoryInfo values(@GoodsId,@Number,@baseId,@GWarningNum,@TotalMoney,'第一次進(jìn)貨',default) set @error+=@@ERROR --記錄有可能產(chǎn)生的錯誤號 end --判斷事務(wù)的提交或者回滾 if(@error<>0) begin rollback transaction return -1 --設(shè)置操作結(jié)果錯誤標(biāo)識 end else begin commit transaction return 1 --操作成功的標(biāo)識 end go
希望本文所述對大家學(xué)習(xí)數(shù)據(jù)庫操作有所幫助。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
上一篇: