更新记录,如果失败,插入记录然后更新

时间:2013-04-11 19:54:40

标签: sql-server stored-procedures insert

我有一个存储过程可以跨多个位置更新库存项目。我最想做的是允许在不同地点之间进行库存转移的正确记录。

我遇到的问题是其中一些地点没有大多数类型的库存物品的记录,所以当我尝试更新数量(从起始位置-1,到新位置+1)时,没有要更新的记录。

这是我的存储过程:

 CREATE  procedure dbo.Inv_Transfer (@p_hStock numeric,
 @p_sFProp varchar(20), @p_hTProp numeric,
 @p_dQuan numeric,  @p_Date datetime,
 @p_sUser1 varchar(1000),@p_sUser2 varchar(1000))
 as
 declare
    @v_FInvhMy numeric,
    @v_TInvhMy numeric,
    @v_sStockCode varchar(10),
    @v_hFProp numeric,
    @v_ibegin int,
    @v_iend int
 begin

     set @v_ibegin  = charindex('(', @p_sFProp  ) 
    if @v_ibegin <= 0 
    begin 
       raiserror('From property string not readable',16,1, @p_sFProp ) 
        return(0)
    end
    else
    begin
       set @v_iend  = charindex (')',@p_sFProp) 
       set @v_hFProp  = substring(@p_sFProp,@v_ibegin + 1,@v_iend - @v_ibegin -1) 
    end  

    select @v_sStockCode = sCode  from mm2stock where hMy = @p_hStock 
    if @@ERROR<> 0
    begin
        raiserror('Stock read failed ',16,1)
        return(0)
    end
     select @v_FInvhMy = hMy from mm2inventory where hStock = @p_hStock and hStoreProp = @v_hFprop 
    if @@ERROR<> 0
    begin
        raiserror('From inventory read failed ',16,1)
        return(0)
    end
     select @v_TInvhMy = hMy from mm2inventory where hstock = @p_hStock and hStoreProp = @p_hTProp 
    if @@ERROR<> 0
    begin
        raiserror('To inventory read failed ',16,1)
        return(0)
    end 
    update mm2inventory set iQtyonHand = iQtyonHand - @p_dquan where hmy = @v_FInvhMy
    if @@ERROR<> 0
    begin
        raiserror('Update from inventory failed ',16,1)
        return(0)
    end         
     update mm2inventory set iQtyonHand = iQtyonHand + @p_dquan where hmy = @v_TInvhMy
    if @@ERROR<> 0
    begin
        raiserror('Update to inventory failed ',16,1)
        return(0)
    end     
    else
    begin
        INSERT INTO mm2inventory (scode, hstock, hstoreprop, dcosteach, dbillprice, ireorderlevel, ireorderqty, iqtyonorder, iqtyonhand, suser1, suser2, suser3, suser4, snotes)
        SELECT (SELECT hmy + 1 where hmy in (select max(hmy) from mm2inventory)),@p_hStock,@p_hTProp,(SELECT dcosteach from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),(SELECT dbillprice from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),(SELECT ireorderlevel from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),(SELECT ireorderqty from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),0,0,'','','','','' FROM mm2inventory

        update mm2inventory set iQtyonHand = iQtyonHand + @p_dquan where hmy = @v_TInvhMy
    end  
     insert into mm2InvXfer( sStock,hinvfrom,hinvto,dquant,dtdate,sUser1,sUser2)
        values (@v_sStockCode,@v_FinvhMy, @v_TInvhMy, @p_dquan,
         isnull(@p_Date,getdate()), @p_sUser1, @p_sUser2) 
    if @@ERROR<> 0
    begin
        raiserror('Insert into inventoryxfer table  failed ',16,1)
        return(0)
    end
 end

摘录我正在努力:

update mm2inventory set iQtyonHand = iQtyonHand - @p_dquan where hmy = @v_FInvhMy
    if @@ERROR<> 0
    begin
        raiserror('Update from inventory failed ',16,1)
        return(0)
    end         
     update mm2inventory set iQtyonHand = iQtyonHand + @p_dquan where hmy = @v_TInvhMy
    if @@ERROR<> 0
    begin
        raiserror('Update to inventory failed ',16,1)
        return(0)
    end     
    else
    begin
        INSERT INTO mm2inventory (scode, hstock, hstoreprop, dcosteach, dbillprice, ireorderlevel, ireorderqty, iqtyonorder, iqtyonhand, suser1, suser2, suser3, suser4, snotes)
        SELECT (SELECT hmy + 1 where hmy in (select max(hmy) from mm2inventory)),@p_hStock,@p_hTProp,(SELECT dcosteach from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),(SELECT dbillprice from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),(SELECT ireorderlevel from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),(SELECT ireorderqty from mm2inventory where hstock = @p_hStock and hstoreprop = @v_hFProp),0,0,'','','','','' FROM mm2inventory

        update mm2inventory set iQtyonHand = iQtyonHand + @p_dquan where hmy = @v_TInvhMy
    end  

因此,正如您在上面所看到的,我正在尝试更新(如果两个存储位置的库存项目都存在记录,则工作正常)但如果出现错误,那么我想为该特定库存插入新行item然后用新的数值更新它,但我做错了。

谁能告诉我我做错了什么? 感谢

1 个答案:

答案 0 :(得分:1)

以下是您可以执行的操作的简单示例,并且您不需要几乎同样多的错误处理(我假设product_code上存在唯一约束):

--Make sure the inventory record exists:

INSERT INTO inventory (product_code, product_name)
SELECT product_code, product_name FROM source s
WHERE NOT EXISTS (
    SELECT product_code 
    FROM inventory i
    WHERE i.product_code = s.product_code)

--Updates the inventory record because you now know it exists:

UPDATE i
SET i.qty = i.qty + s.qty --obviously change sign where appropriate
FROM inventory i
JOIN source s ON s.product_code = i.product_code

这样做的目的是每次尝试INSERT,但它会自然地过滤掉已存在的任何内容。如果INSERT没有任何内容,则不会发生任何事情。然后,您可以轻松追求UPDATE