欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SQLServer存儲過程中事務(wù)的使用方法

 更新時間:2015年12月13日 14:53:02   作者:iceKnight  
這篇文章主要介紹了SQLServer存儲過程中事務(wù)的使用方法,簡短的代碼帶大家更好的學(xué)習(xí)使用SQLServer存儲過程中事務(wù),感興趣的小伙伴們可以參考一下

本文為大家分享了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
    --實現(xiàn)進貨信息的添加
    insert into StockInfo values(@GoodsId, @Number, @StockPrice, @SupplierId, @EmpId, @StockUnit, @StockDate, @TotalMoney, @ActMoney,DEFAULT,@Description, @baseId)
    set @error+=@@ERROR --記錄有可能產(chǎ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 --這個商品從來沒有過進貨記錄,那么就應(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,'第一次進貨',default)
        set @error+=@@ERROR --記錄有可能產(chǎn)生的錯誤號      
      end
--判斷事務(wù)的提交或者回滾
if(@error<>0)
  begin
    rollback transaction
    return -1 --設(shè)置操作結(jié)果錯誤標識
  end
else
  begin
    commit transaction
    return 1 --操作成功的標識
  end
go

希望本文所述對大家學(xué)習(xí)數(shù)據(jù)庫操作有所幫助。

相關(guān)文章

最新評論