

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

CREATE PROC PReplaceItemId
(
 @OldId BIGINT,
 @NewId BIGINT
)
AS
BEGIN 
  
    UPDATE TConsumeItem SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TConsumeItemCostGoods SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TConsumeReturnVisit SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TCourse SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TCourseContainItem SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TCourseReplaceItem SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TEmpPerfDetail# SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TGoodsFreeItem SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TItemBaseComm SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemBaseCommSection SET FIntemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemCommSchemeCost SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemCommSchemeDetail SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemCommSchemeExcept SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemCommSchemeExtra SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemCostGoods SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemDiscountMaster SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TItemReturnVisit SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TItemSub SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TMemberCardTypeAllowItem SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TMemberCardTypeFreeItem SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TMemberCourse SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TMemberCourseLog SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TMemberSaleCourse SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TMemberSaleFreeItem SET FItemId = @NewId WHERE FItemId = @NewId 
    UPDATE TOrderItem SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TOrderItemCostGoods SET FItemId = @NewId WHERE FItemId = @NewId
    UPDATE TQuickItem SET FItemIds = REPLACE(FItemIds,CAST(@OldId AS VARCHAR),CAST(@NewId AS VARCHAR))   WHERE FItemIds LIKE '%' + CAST(@NewId AS VARCHAR) + '%'

	UPDATE TMemberArrear SET FContentId = @NewId WHERE FContentId=@OldId AND FContentType = 'Item'
    UPDATE TMemberSaleRepay SET FArrearContentId = @NewId WHERE FArrearContentId = @NewId AND FArrearContentType = 'Item'
	UPDATE TMemberTransHist SET FContentId = @NewId WHERE FContentId=@OldId AND FBizType = 'Item'
	UPDATE TItem SET FId = @NewId WHERE FId=@OldId  

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

BEGIN TRAN


EXEC PReplaceItemId 5709193178895249005,8399417444818458580
EXEC PReplaceItemId 6163119302417216778,6935985745057864392
EXEC PReplaceItemId 7931619969514830516,8039179144496772961
EXEC PReplaceItemId 6987717798158889021,5720913661237724970
EXEC PReplaceItemId 5207403414622550175,9008300454355072994
EXEC PReplaceItemId 5909855031858546807,5977577486545951824
EXEC PReplaceItemId 6284991151989738835,6192880138600725629
EXEC PReplaceItemId 4935168455371168244,7903142775467419521
EXEC PReplaceItemId 4952994180896738861,6652231722341617714
EXEC PReplaceItemId 8492726973662401498,8228600572271696614
EXEC PReplaceItemId 5889028406897097913,8678505482699623808
EXEC PReplaceItemId 4629967776589329968,8791890210055148197
EXEC PReplaceItemId 6426400603866669629,8878567748645574019
EXEC PReplaceItemId 7000265299934312520,8049687640307166159