-- Schema: C6MartPeriodico -- Stored Procedure: PL_D_S178CasiParticolari -- [C6MartPeriodico].[PL_D_S178CasiParticolari] 'F','BLRDNC73D12B925K' CREATE procedure [C6MartPeriodico].[PL_D_S178CasiParticolari] --'S','02708090580' -- Add the parameters for the stored procedure here @Rete char(1), @CodiceFiscale varchar(16) AS BEGIN declare @Copertura decimal(10,2) declare @SoloProdottiSenzaAdeguatezza int declare @SoloCC int declare @SoloProdottiSenzaAdeguatezzaOrSoloCC int declare @Scaduti_Retail varchar declare @Scaduti_Prof varchar declare @IDTesto int declare @isDirect varchar set @IDTesto = 0 ------------------------------------------------------------------------------ CASO 1 COPERTURA = 0 select @isDirect = case when TIP_CONTRATTO=3 then 'S' else 'N' end from C6StagingPeriodico.APPO_CONTRATTI where rete=@Rete and COD_FISCALE=@CodiceFiscale select @Copertura=copertura from C6MartPeriodico.RISCHIO_AGGREGATO -- 'COMPLESSIVO|BF' come viene fatto nella PL_D_S157ConfrontoPiramidi per la copertura_tot, campo per l'esposizione della nota sulla coperura sotto la piramide where cod_aggreg='COMPLESSIVO|BF' AND COD_FISCALE = @COdiceFISCALE and RETE= @RETE ------------------------------------------------------------------------------ FINE CASO 1 ------------------------------------------------------------------------------ CASO 2 SENZA PRODOTTI IN ADEGUATEZZA E/O SOLO CC IN PORTAFOGLIO -- Controllo se nel Portafoglio c'è solo CC select rete, cod_fiscale, id_area into #t1 from c6martperiodico.patrimonio_bf where rete = @Rete and cod_fiscale = @CodiceFiscale group by rete,cod_fiscale, id_area select rete, cod_fiscale, count(*) as numero into #t2 from #t1 group by rete, cod_fiscale having count(*) = 1 if exists( select p.rete, p.cod_fiscale from c6martperiodico.patrimonio_bf p inner join #t2 on p.rete = #t2.rete and p.cod_fiscale = #t2.cod_fiscale --where rete + cod_fiscale in (select rete + cod_fiscale from #t2) where p.id_area = 'liq' and p.tipo_prodotto = 'cc' ) set @soloCC = 1 else set @soloCC = 0 drop table #t1 drop table #t2 -- Se le count torna 0 record significa che in ptf ci sono solo prodotti senza adeguatezza select @SoloProdottiSenzaAdeguatezza = count(*) from c6martperiodico.patrimonio_bf patr left join [C6StagingPeriodico].[Prodotti_Fuori_Adeguatezza] senza on patr.cod_interno = senza.codinterno and patr.cod_sottoprodotto=senza.codsottoprodotto where patr.rete = @rete and patr.cod_fiscale = @codicefiscale and senza.codinterno is null -- Se si verifica uno dei due casi, imposto la variabile a 1 if @SoloProdottiSenzaAdeguatezza = 0 or @SoloCC = 1 set @SoloProdottiSenzaAdeguatezzaOrSoloCC = 1 else set @SoloProdottiSenzaAdeguatezzaOrSoloCC = 0 ----------------------------------------------------------------------------- FINE CASO 2 -- CASO 3 COPERTURA = 0 + e ptf con solo PRODOTTI senza ADEGUATEZZA -- FINE CASO 3 --4 Scaduti Retail select @Scaduti_Retail = RPF.FLGPROF from C6MartPeriodico.wseirpf RPF left join C6StagingPeriodico.WSEIAN2 w on w.RETE=RPF.Rete and w.CODFIS=RPF.CODFIS where RPF.FLGPROF='P' and W.FLGPROF='N' AND RPF.CODFIS = @COdiceFISCALE and RPF.RETE= @RETE --5 Scaduti Prof select @Scaduti_Prof = RPF.FLGPROF from C6MartPeriodico.wseirpf RPF left join C6StagingPeriodico.WSEIAN2 w on w.RETE=RPF.Rete and w.CODFIS=RPF.CODFIS where RPF.FLGPROF='P' and W.FLGPROF='S' AND RPF.CODFIS = @COdiceFISCALE and RPF.RETE= @RETE -- RISULTATO FINALE if(@Copertura = 0) begin --if(@SoloProdottiSenzaAdeguatezza=0) if(@SoloProdottiSenzaAdeguatezzaOrSoloCC = 1) begin set @IDTesto = 3 end else begin set @IDTesto = 1 end end else --if(@SoloProdottiSenzaAdeguatezza=0) if(@SoloProdottiSenzaAdeguatezzaOrSoloCC = 1) begin set @IDTesto = 2 end if (@Scaduti_Retail) ='P' and (@isDirect=