CREATE procedure [C6MartPeriodico].[RP_DM_W6IndPtf] as begin declare @countAppoLimiter int select @countAppoLimiter= count(*) from C6StagingPeriodico.APPO_CONTRATTI_LIMITER drop table if exists #tot if(@countAppoLimiter=0) begin truncate table C6MartPeriodico.W6IndPtf INSERT INTO [C6MartPeriodico].[W6IndPtf] ([DTTRIM] ,[RETE] ,[CHIAVE_ACN] ,[CODFIS] ,[CODMAN] ,[PTF_DIVERSIFICATO] ,[PTF_ADEGUATO] ,[BLACKLIST] ,[DATA_ORA_INSE] ,[DATA_ORA_AGGI]) select ad.Dttrim, ad.Rete, ad.Chiave_Acn as Chiave_Acn, ad.Codfis, ad.Codman, case when isnull(ptf.flg_NonDiv,'N')='S' then 'N' else 'S' end as flg_NonDiv, ad.adeg_check, case when e.rete IS not null then 'S' else 'N' end as Blacklist, NULL as DATA_ORA_INSE, NULL as DATA_ORA_AGGI from C6MartPeriodico.W6CKADEG ad inner join C6StagingPeriodico.appo_contratti appo on appo.rete=ad.Rete and appo.cod_fiscale=ad.Codfis left join C6MartPeriodico.RP_ClientiPtfNonDiv ptf on ad.Rete=ptf.rete and ad.Chiave_Acn=ptf.chiave_acn left join (select distinct rete,codicefiscale from C6MartPeriodico.ExcludedList) e on ad.Rete=e.Rete and ad.Chiave_Acn=e.codicefiscale WHERE appo.tip_contratto<>'3' end --select distinct * from C6MartPeriodico.W6IndPtf else delete from [C6MartPeriodico].[W6IndPtf] where rete+CHIAVE_ACN in (select rete+cod_fiscale from C6StagingPeriodico.APPO_CONTRATTI_LIMITER) INSERT INTO [C6MartPeriodico].[W6IndPtf] ([DTTRIM] ,[RETE] ,[CHIAVE_ACN] ,[CODFIS] ,[CODMAN] ,[PTF_DIVERSIFICATO] ,[PTF_ADEGUATO] ,[BLACKLIST] ,[DATA_ORA_INSE] ,[DATA_ORA_AGGI]) select ad.Dttrim, ad.Rete, ad.Chiave_Acn as Chiave_Acn, ad.Codfis, ad.Codman, case when isnull(ptf.flg_NonDiv,'N')='S' then 'N' else 'S' end as flg_NonDiv, ad.adeg_check, case when e.rete IS not null then 'S' else 'N' end as Blacklist, NULL as DATA_ORA_INSE, NULL as DATA_ORA_AGGI from C6MartPeriodico.W6CKADEG ad inner join C6StagingPeriodico.APPO_CONTRATTI_LIMITER appo_lim on appo_lim.rete=ad.Rete and appo_lim.cod_fiscale=ad.Codfis left join C6StagingPeriodico.APPO_CONTRATTI appo on appo.rete=ad.Rete and appo.cod_fiscale=ad.Codfis left join C6MartPeriodico.RP_ClientiPtfNonDiv ptf on ad.Rete=ptf.rete and ad.Chiave_Acn=ptf.chiave_acn left join (select distinct rete,codicefiscale from C6MartPeriodico.ExcludedList) e on ad.Rete=e.Rete and ad.Chiave_Acn=e.codicefiscale WHERE appo.tip_contratto<>'3' --SI --NO --NA Non Applicabile --NR Non restituito, Professionale END