IF OBJECT_ID('PReplaceGoodsId') IS NOT NULL
DROP PROC PReplaceGoodsId
GO

CREATE PROC PReplaceGoodsId
(
 @OldId BIGINT,
 @NewId BIGINT
)
AS
BEGIN 
 
	UPDATE TConsumeGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId 
	UPDATE TConsumeItemCostGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TCourseGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TGoodsBaseComm SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TGoodsCommSchemeDetail SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TGoodsDiscountMaster SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TGoodsFreeItem SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TGoodsMultiUnit SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TGoodsSub SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TInventory SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TInventoryBatch SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TInventoryLog SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TItemCostGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMallGoodsCollage SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMallGoodsRush SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMallGoodsSub SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMallOrderBill SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMemberCourseGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMemberDepositGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMemberGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMemberSaleCourseGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TOrderGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TOrderItemCostGoods SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TStockInDetail SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TStockOutDetail SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TTakeStockDetail SET FGoodsId=@NewId WHERE FGoodsId=@OldId
	UPDATE TMemberArrear SET FContentId = @NewId WHERE FContentId=@OldId AND FContentType = 'Goods'
	UPDATE TMemberTransHist SET FContentId = @NewId WHERE FContentId=@OldId AND FBizType = 'Goods'
	UPDATE TMemberSaleRepay SET FArrearContentId = @NewId WHERE FArrearContentId = @NewId AND FArrearContentType = 'Goods'
	UPDATE TGoods SET FId = @NewId WHERE FId=@OldId  

     
    IF OBJECT_ID('TGoodsIdReplaceLog') IS NULL
    BEGIN
    EXEC('
    CREATE TABLE TGoodsIdReplaceLog 
    (
        FOldGoodsId BIGINT  ,
        FNewGoodsId BIGINT,
        FTime DATETIME,
		CONSTRAINT PK_GOODS_ID_REPLACE_LOG PRIMARY KEY (FOldGoodsId)
    )
     
    ')
    END
    
    DECLARE @Sql VARCHAR(1000)
    SELECT @Sql = 'INSERT TGoodsIdReplaceLog SELECT ' + CAST(@OldId AS VARCHAR)+','+CAST(@NewId AS VARCHAR) + ',GETDATE()'
    
    EXEC(@Sql)
END
GO



