USE [HCITools]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CleanUp_Objects_Security_History]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CleanUp_Objects_Security_History]
GO

USE [HCITools]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CleanUp_Objects_Security_History]   
                       @in_debug         tinyint     = 0
AS 
/*============================================================================= 

  Explication du traitement realise par la SP
  -------------------------------------------
  This SP will delete objects security history according to type of environnement. 
 
  Context
  ----------------------
    Call by job W93140 - Backup objects security
    
  Parameters  
  ----------  
    @in_debug : not used 
      
  Create date : 05.10.2018 / RTC
  
=============================================================================*/ 
 
set nocount on;
 
/*------------------- Declaration des variables --------------------*/
 
declare @Oldest_date   datetime

BEGIN TRY

    IF EXISTS (   SELECT *
                    FROM [master].[cfg].[InstanceContext]
                   WHERE Type in ( 'VALI', 'DEV' ))
        SELECT @Oldest_date = dateadd(m, -3, getdate())
    ELSE
        SELECT @Oldest_date = dateadd(m, -6, getdate())

    DELETE FROM [HCITools].[dba].[Principal_Permission]
     WHERE [PP_timestamp] < @Oldest_date

/*---------------------- Traitement des erreurs ----------------------*/
END TRY
BEGIN CATCH

    /* Error management with RaiseError */
    EXEC dbo.get_Error_Info @in_RaiseError = 1

END CATCH

GO
