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

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

 
	UPDATE TStockInBill SET FOperatorId = @NewId WHERE FOperatorId = @OldId
	UPDATE TTakeStockBill SET FOperatorId = @NewId WHERE FOperatorId = @OldId
	UPDATE TStockOutBill SET FOperatorId = @NewId WHERE FOperatorId = @OldId
	UPDATE TStockOutBill SET FReceiverId = @NewId WHERE FReceiverId = @OldId

	UPDATE TMemberSaleEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
   
    UPDATE TEmployeeExperience SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TCouponCode SET  FIssueEmpId=@NewId WHERE FIssueEmpId = @OldId  
    UPDATE TMemberCourse SET  FBizEmpId=@NewId WHERE FBizEmpId = @OldId 
    UPDATE TMember SET  FChargeEmpId=@NewId WHERE FChargeEmpId = @OldId 
    UPDATE TOrderSaleEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TUser SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TRegistrationEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeBookingItem SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TInvention SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TMemberArrear SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeCommScheme SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeSub SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TMemberArrearEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    
    UPDATE TMemberCard SET  FBizEmpId=@NewId WHERE FBizEmpId = @OldId 
    UPDATE TOrderItemEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeReferral SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeReferral SET  FRefEmpId=@NewId WHERE FRefEmpId = @OldId 
    UPDATE TConsumeReturnVisit SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TMemberFollow SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TConsumeBill SET  FOwnerEmpId=@NewId WHERE FOwnerEmpId = @OldId 
    UPDATE TBookingRequest SET  FEmpId=@NewId WHERE FEmpId = @OldId  
    UPDATE TEmployeeAllowCompany SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TBooking SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeAttend SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TBillEvaluation SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeAttendLog SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployeeExpenses SET  FEmpId=@NewId WHERE FEmpId = @OldId  
    UPDATE TEmployeeProperty SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TConsumeItemEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
   
    UPDATE TMemberSaleBill SET  FOwnerEmpId=@NewId WHERE FOwnerEmpId = @OldId 
   
    UPDATE TEmployeeSchedule SET  FEmpId=@NewId WHERE FEmpId = @OldId  
   
    UPDATE TMemberEmployee SET  FEmpId=@NewId WHERE FEmpId = @OldId 
    UPDATE TEmployee SET  FId=@NewId WHERE FId = @OldId 
     
    
   IF OBJECT_ID('TEmployeeIdReplaceLog') IS NULL
   BEGIN
   EXEC('
   CREATE TABLE TEmployeeIdReplaceLog 
   (
      FOldEmployeeId BIGINT  ,
      FNewEmployeeId BIGINT,
      FTime DATETIME,
      CONSTRAINT PK_Employee_ID_REPLACE_LOG PRIMARY KEY (FOldEmployeeId)
   )
    
   ')
   END

   DECLARE @Sql VARCHAR(1000)
   SELECT @Sql = 'INSERT TEmployeeIdReplaceLog SELECT ' + CAST(@OldId AS VARCHAR)+','+CAST(@NewId AS VARCHAR) + ',GETDATE()'

   EXEC(@Sql)
END
GO



  