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 --設置操作結果錯誤標識 endelse begin commit transaction return 1 --操作成功的標識 endgo
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。