DROP PROC PReplaceCardTypeId
GO

CREATE PROC PReplaceCardTypeId
(
 @OldId BIGINT,
 @NewId BIGINT
)
AS
BEGIN 

    UPDATE TMemberCardType SET FId = @NewId WHERE FId = @OldId
    UPDATE TMemberCardTypeSub SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCard  SET FTypeId = @NewId WHERE FTypeId = @OldId
    UPDATE TMemberSaleCard  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberSaleCard  SET FNewCardTypeId = @NewId WHERE FNewCardTypeId = @OldId
    UPDATE TMemberCardAcctHist  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardOldAcctHist  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TConsumeBill  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TConsumeItemPayment  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TConsumeGoodsPayment  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberSaleBill  SET FPayCardTypeId = @NewId WHERE FPayCardTypeId = @OldId
    UPDATE TItemDiscountMid  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TCardTypeCommSchemeSection  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardTypeFreeItem  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardTypeAccount  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardTypeAllowItem  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardTypeBaseComm  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardTypeCross  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberCardTypeCoursePack  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TGoodsDiscountDetail  SET FCardTypeId = @NewId WHERE FCardTypeId = @OldId
    UPDATE TMemberTransHist SET FContentId=@NewId WHERE FContentId=@OldId AND FBillType = 'KK' 
    UPDATE TMemberCardTypeShare SET FCardTypeId=@NewId WHERE FCardTypeId=@OldId  

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