CREATE FUNCTION [dbo].[Cc_Email] ( @RETE as varchar(1) = '', @CODICE as varchar(6) = '', @NS as int = 0, -- NUMERO SOLLECITI FATTI, @TIPOEMAIL as varchar(19) = '' ) RETURNS varchar(500) AS BEGIN DECLARE @CC_DI_RITORNO as varchar(500) SET @CC_DI_RITORNO = ''; if @TIPOEMAIL = 'PERF' RETURN @CC_DI_RITORNO declare @flagSEDE as bit -- uso questo flag per allegare gli indirizzi di SEDE set @flagSEDE = 0 declare @flagRMDM as bit -- uso questo flag per spedire ad RM,DM set @flagRMDM = 0 DECLARE @PV as varchar(1) SET @PV = ';' declare @flagAM as bit -- uso questo flag per spedire ad AM set @flagAM = 0 DECLARE @REF_SEDE as varchar(200) SET @REF_SEDE = '' IF @RETE = 'F' BEGIN SET @REF_SEDE = @REF_SEDE + 'SEIDIREZIONERETE@BANCAFIDEURAM.IT' + @PV; SET @REF_SEDE = @REF_SEDE + 'SEI@FIDEURAM.IT'; END ELSE IF @RETE = 'S' BEGIN SET @REF_SEDE = @REF_SEDE + 'SEIDIREZIONERETE@SPINVEST.COM' + @PV; SET @REF_SEDE = @REF_SEDE + 'SEI@FIDEURAM.IT'; END -- 1_SOLL_NOPRO: non sono previsti indirizzi in cc IF @TIPOEMAIL = '1_SOLL_NOPRO' BEGIN SET @flagRMDM = 0; SET @flagSEDE = 0; SET @flagAM = 0; END -- 2_SOLL_NOPRO: divisional manager; regional manager (dm, rm) IF @TIPOEMAIL = '2_SOLL_NOPRO' BEGIN SET @flagRMDM = 1; SET @flagSEDE = 0; SET @flagAM = 0; END -- 3_SOLL_NOPRO: area manager; divisional manager; regional manage (am,dm,rm) IF @TIPOEMAIL = '3_SOLL_NOPRO' BEGIN SET @flagRMDM = 1; SET @flagAM = 1; SET @flagSEDE = 0; END -- S_SOLL_NOPRO/REVOCA_NOPRO (am,dm,rm): (RETE F) area manager, divisional manager, regional manager, SEIDIREZIONERETE@BANCAFIDEURAM.IT, SEI@FIDEURAM.IT -- (RETE S) area manager, divisional manager, regional manager, SEIDIREZIONERETE@SPINVEST.COM, SEI@FIDEURAM.IT IF @TIPOEMAIL IN ('S_SOLL_NOPRO','REVOCA_NOPRO') BEGIN SET @flagRMDM = 1; SET @flagAM = 1; SET @flagSEDE = 1; END declare @ritorno as varchar(500) set @ritorno = '' IF(@flagRMDM=1) begin --em.rete,em.codice, --em.email select @ritorno = @ritorno + em.email + @PV from ( SELECT rete,rm as codice from c6mart.struttura_rete sr where Rete = @RETE and PB = @CODICE and rm <> pb union all SELECT rete,dm as codice from c6mart.struttura_rete sr where Rete = @RETE and PB = @CODICE and dm <> rm ) a inner join C6Mart.EMAIL_PB em on a.rete = em.rete and a.codice = em.codice end IF(@flagAM=1) begin --em.rete,em.codice, --em.email select @ritorno = @ritorno + em.email + @PV from ( SELECT rete,am as codice from c6mart.struttura_rete sr where Rete = @RETE and PB = @CODICE and am <> dm and @flagAM = 1 ) a inner join C6Mart.EMAIL_PB em on a.rete = em.rete and a.codice = em.codice end --dal 3 sollecito coinvolgo anche Direzione ed Assistenza Rete if(@flagSEDE=1) set @ritorno = @REF_SEDE + @PV + @ritorno RETURN @ritorno --RETURN '@NS=' + cast(@NS as varchar(2)) + '-' + '@flagSEDE=' + cast(@flagSEDE as varchar(2))+ '-' + '@flagAM=' + cast(@flagAM as varchar(2)) END