-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
-- [C6MartPeriodico].[PL_S94Rischio] 'F','GRZDSG43E11D193N'
CREATE procedure [C6MartPeriodico].[PL_S94Rischio]
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @varBF DECIMAL(12,2)
	DECLARE @coperturaBF DECIMAL(12,2)
	DECLARE @codiceProfilo SMALLINT
	DECLARE @dataProfilo DATETIME
	DECLARE @varProfilo DECIMAL(5,2)
	DECLARE @nomeProfilo VARCHAR(50)
	SELECT
		@varBF = VAR_PERC_PTF,
		@coperturaBF = COPERTURA
	FROM
		C6MartPeriodico.RISCHIO_AGGREGATO
	WHERE
		RETE = @Rete AND
		COD_FISCALE = @CodiceFiscale AND
		COD_AGGREG = 'COMPLESSIVO|BF'
	SELECT
        @codiceProfilo = M.PROFILO_ASS,
		@dataProfilo = M.DATA_INIZIO_VAL,
		@varProfilo = CR.MAX_VAR,
		@nomeProfilo = CR.DESCRIZIONE
	FROM
		C6MartPeriodico.MIFID AS M
		INNER JOIN C6MartPeriodico.CODIFICA_RISCHIO AS CR
		ON M.PROFILO_ASS = CR.PROFILO
	WHERE
		M.RETE = @Rete AND
		M.COD_FISCALE = @CodiceFiscale
	SELECT
        @codiceProfilo as codiceProfilo,
		@dataProfilo as dataProfilo,
		@varProfilo as varProfilo,
		ISNULL(@varBF,0) as varBF,
		ISNULL(@coperturaBF,0) as coperturaBF,
		@nomeProfilo as nomeProfilo
END