-- Schema: C6StagingPeriodico
-- Stored Procedure: SRV_STORICIZZAZIONE


-- =============================================
-- Author:		paolo giovanetti
-- Create date: 6 marzo 2009
-- Description:	storicizzazione STAGING
-- =============================================
CREATE procedure [C6StagingPeriodico].[SRV_STORICIZZAZIONE]
	@NomeTabella VARCHAR(50) = NULL
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @ID_ELAB INT			-- Elaborazione da storicizzare
	DECLARE @MIN_ID_ELAB AS INT		-- Elaborazione vecchia da eliminare dallo storico
	DECLARE @TempoStoricoC6 AS INT	-- Numero di giorni di vita dello storico espresso in giorni

	SET @TempoStoricoC6 = dbo.getparametro('TempoStoricoC6') 

	SELECT @MIN_ID_ELAB = ISNULL(MAX(ID_ELAB),0)
	FROM C6MartPeriodico.ELABORAZIONI
	WHERE DATEDIFF(DAY,DATA_OPERAZIONE,GETDATE()) > @TempoStoricoC6

	SET @ID_ELAB = C6MartPeriodico.GETIDELAB()

-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TABELLE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	--XXXXXXXXXXXXXXXXXXXXXXXX ACBDENOTH XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ACBDENOTH')
	BEGIN
		DELETE FROM C6StoricoPeriodico.ST_ACBDENOTH WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
		INSERT INTO C6StoricoPeriodico.ST_ACBDENOTH
		(OID$, VERSION$, BDPERCENTAGE, BDBETA, F_OID$_PASSE_CDF1A, F_PFINANCIAL_815C0, ID_ELAB)
		SELECT 
		 ACBDENOTH.OID$, ACBDENOTH.VERSION$, ACBDENOTH.BDPERCENTAGE, ACBDENOTH.BDBETA, ACBDENOTH.F_OID$_PASSE_CDF1A, ACBDENOTH.F_PFINANCIAL_815C0, @ID_ELAB
		FROM C6StagingPeriodico.ACBDENOTH
		LEFT OUTER JOIN C6StoricoPeriodico.ST_ACBDENOTH
			ON ACBDENOTH.OID$ = ST_ACBDENOTH.OID$
		WHERE ST_ACBDENOTH.OID$ IS NULL
	END
 
	--XXXXXXXXXXXXXXXXXXXXXXXX ACBDENTRY XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ACBDENTRY')
	BEGIN
		DELETE FROM C6StoricoPeriodico.ST_ACBDENTRY WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
		INSERT INTO C6StoricoPeriodico.ST_ACBDENTRY
		(OID$, VERSION$, F_PINDEXBREA_08079, F_PINSTRUMEN_FBCDE, BDPERCENTAGE, BDBETA, F_OID$_PASSE_B8A1E, F_PFINANCIAL_65EBB, F_PFINANCIAL_C2FD3, ID_ELAB)
		SELECT 
		 ACBDENTRY.OID$, ACBDENTRY.VERSION$, ACBDENTRY.F_PINDEXBREA_08079, ACBDENTRY.F_PINSTRUMEN_FBCDE, ACBDENTRY.BDPERCENTAGE, ACBDENTRY.BDBETA, ACBDENTRY.F_OID$_PASSE_B8A1E, ACBDENTRY.F_PFINANCIAL_65EBB, ACBDENTRY.F_PFINANCIAL_C2FD3, @ID_ELAB
		FROM C6StagingPeriodico.ACBDENTRY
		LEFT OUTER JOIN C6StoricoPeriodico.ST_ACBDENTRY
			ON ACBDENTRY.OID$ = ST_ACBDENTRY.OID$
		WHERE ST_ACBDENTRY.OID$ IS NULL
	END
 
	--XXXXXXXXXXXXXXXXXXXXXXXX ADBKBASE XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ADBKBASE')
	BEGIN
		DELETE FROM C6StoricoPeriodico.ST_ADBKBASE WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
		INSERT INTO C6StoricoPeriodico.ST_ADBKBASE
		(OID$, VERSION$, ADBKID, INSTID, C_ADBKBASE_L_69013, TYPE$_ADBKST_AC419, C_CLIENTNAME_1A67C, C_CLIENTNAME_1E844, F_OID$_PERSO_06FCB, C_CLIENTNAME_8B1DB, C_CLIENTNAME_B4F69, C_CLIENTNAME_EA63A, C_ADBKBASE_D_C669E, C_ADBKBASE_L_1DD83, C_ADBKBASE_L_F8E21, C_ADBKBASE_L_2805C, C_ADBKBASE_T_C94F1, F_OID$_PROFI_98D05, F_OID$_TAXRE_4AC4C, C_CURRENCYSY_36ED7, F_OID$_REFER_66AC8, C_ADBKBASE_M_AE5A8, C_ADBKBASE_A_36B89, C_CURRENCYSY_0483B, C_VALUE_506AF, F_OID$_SOURC_E66C5, F_OID$_QUALI_027C5, F_OID$_BLOCK_A4386, F_OID$_SOURC_3AA10, TYPE$_ADBKTY_20093, F_PCUSTOMGRO_9BC08, F_PENTITY_AD_29FE1, ID_ELAB)
		SELECT 
		 ADBKBASE.OID$, ADBKBASE.VERSION$, ADBKBASE.ADBKID, ADBKBASE.INSTID, ADBKBASE.C_ADBKBASE_L_69013, ADBKBASE.TYPE$_ADBKST_AC419, ADBKBASE.C_CLIENTNAME_1A67C, ADBKBASE.C_CLIENTNAME_1E844, ADBKBASE.F_OID$_PERSO_06FCB, ADBKBASE.C_CLIENTNAME_8B1DB, ADBKBASE.C_CLIENTNAME_B4F69, ADBKBASE.C_CLIENTNAME_EA63A, ADBKBASE.C_ADBKBASE_D_C669E, ADBKBASE.C_ADBKBASE_L_1DD83, ADBKBASE.C_ADBKBASE_L_F8E21, ADBKBASE.C_ADBKBASE_L_2805C, ADBKBASE.C_ADBKBASE_T_C94F1, ADBKBASE.F_OID$_PROFI_98D05,
