-- Schema: C6Mart
-- Stored Procedure: RP_DM_WSEICAP





  
-- =============================================  
-- Author:  Paolo Giovanetti  / TRINGALI PER FIDUCIARIE  
-- Create date: <Create Date,,>  
-- Description: <Description,,>  
-- =============================================  
CREATE procedure [C6Mart].[RP_DM_WSEICAP]  
AS  
BEGIN  
  
 SET NOCOUNT ON;  
  

  
 -- Log esecuzione  
 INSERT INTO LOG_ESECUZIONE  
  (Nome, Inizio, Nota)  
 VALUES (  
  'RP_DM_WSEICAP',  
  GETDATE(),  
  'Elaborazione trimestrale'  
 )  
  
 TRUNCATE TABLE C6Mart.WSEICAP 
  
 INSERT INTO C6Mart.[WSEICAP]  
           ([DTTRIM]
      ,[DTRIFE]
      ,[RETE]
      ,[COD_FISCALE]
      ,[CODCONF]
      ,[SUBPROD]
      ,[ISIN]
      ,[DTINIZ]
      ,[DTFINE]
      ,[CTV_COMMIT]
      ,[IMPVERS]
      ,[IMPRES]
      ,[DESCPROD]
      ,[SGR])  

 select 
 '99999999' as DTTRIM,
 '99999999' as DTRIFE,
PATRBF.rete as RETE,
PATRBF.COD_FISCALE as COD_FISCALE,
PATRBF.ID_CONTRATTO as CODCONF,
PATRBF.COD_SOTTOPRODOTTO as SUBPROD,
PATRBF.ISIN as ISIN,
ICAP.dataInizioCommitment as DTINIZ,
ICAP.dataFineCommitment as DTFINE,
ICAP.totaleCommitment as CTV_COMMIT,
ICAP.importoVersato as IMPVERS,
ICAP.importoDaVersare as IMPRES,
ANAG.DESCR_PRODOTTO as DESCPROD,
ANAG.DESCR_SGR as SGR
from C6Mart.PATRIMONIO_BF PATRBF
inner join C6Staging.SPB_wseicap ICAP on PATRBF.ID_CONTRATTO=ICAP.codicecontratto 
and PATRBF.ISIN=ICAP.isin
left join C6MART.ANAG_PRODOTTI ANAG
ON ANAG.COD_PRODOTTO = PATRBF.COD_PRODOTTO
 -- Log esecuzione  
 UPDATE  
  LOG_ESECUZIONE  
 SET  
  Fine = GETDATE(),  
  Tipo = 'INSERT',  
  Righe = @@ROWCOUNT  
 WHERE  
  Nome = 'RP_DM_WSEICAP' AND  
  Inizio = (  
   SELECT  
    MAX(Inizio)  
   FROM  
    LOG_ESECUZIONE  
   WHERE  
    Nome = 'RP_DM_WSEICAP'  
  )  
  
END  
  

