CREATE procedure [C6MartPeriodico].[DM_BONUS]  
AS  
BEGIN  
 SET NOCOUNT ON;  
  DECLARE @ID_ELAB INT  
 DECLARE @TIPO_ELAB VARCHAR(1)  
    DECLARE @DTMAX INT  
 SET @ID_ELAB = C6MartPeriodico.GETIDELAB()  
 SET @TIPO_ELAB = C6MartPeriodico.GETTIPOELAB()  
    SELECT  @DTMAX = ISNULL(MAX(DTTRIM),0) FROM c6StagingPeriodico.WSEIBO     
 -- Log esecuzione  
 INSERT INTO LOG_ESECUZIONE_PERIODICO  
  (Nome, Inizio, Nota)  
 VALUES (  
  'DM_BONUS',  
  GETDATE(),  
  'Elaborazione trimestrale'  
 )  
 TRUNCATE TABLE [C6MartPeriodico].[BONUS]  
 INSERT INTO [C6MartPeriodico].[BONUS]  
           (   
   [RETE],  
   [COD_FISCALE],      
   [CTV],  
   [DT_TRIM],    
   [BONUS_M],  
   [DTDEC_M],   
   [BONUS_5],  
   [DTDEC_5],   
   [BONUS_10],  
   [DTDEC_10],  
   [COD_CONF],   
   [COD_PROD]  
   )  
 SELECT   
   WSEIBO.[RETE] as [RETE],  
   WSEIBO.[CODFIS] as [COD_FISCALE],  
   WSEIBO.[CTV] as [CTV],  
   convert(datetime,convert(varchar,WSEIBO.[DTTRIM]),100) as [DT_TRIM],    
   WSEIBO.[BONUSM] as [BONUS_M],  
   convert(datetime,convert(varchar,WSEIBO.[DTDECM]),100) as [DTDEC_M],   
   WSEIBO.[BONUS5] as [BONUS_5],  
   convert(datetime,convert(varchar,WSEIBO.[DTDEC5]),100) as [DTDEC_5],   
   WSEIBO.[BONUS10] as [BONUS_10],  
   convert(datetime,convert(varchar,WSEIBO.[DTDEC10]),100) as [DTDEC_10],     
   WSEIBO.[CODCONF] as [COD_CONF],  
   WSEIBO.[CODPROD] as [COD_PROD]  
 FROM   
  c6StagingPeriodico.WSEIBO    
 WHERE   
   WSEIBO.[DTTRIM] = @DTMAX  
 -- Log esecuzione  
 UPDATE  
  LOG_ESECUZIONE_PERIODICO  
 SET  
  Fine = GETDATE(),  
  Tipo = 'INSERT',  
  Righe = @@ROWCOUNT  
 WHERE  
  Nome = 'DM_BONUS' AND  
  Inizio = (  
   SELECT  
    MAX(Inizio)  
   FROM  
    LOG_ESECUZIONE_PERIODICO  
   WHERE  
    Nome = 'DM_BONUS'  
  )  
END