-- Schema: C6Staging
-- Stored Procedure: SRV_STORICIZZAZIONE_OLD

-- [C6Staging].[SRV_STORICIZZAZIONE] 'ACBDENOTH'
CREATE PROCEDURE [C6Staging].[SRV_STORICIZZAZIONE_OLD]
	@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

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

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

	SET @ID_ELAB = C6MART.GETIDELAB()

-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TABELLE XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	--XXXXXXXXXXXXXXXXXXXXXXXX ACBDENOTH XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ACBDENOTH')
		BEGIN
			DELETE FROM C6Storico.ST_ACBDENOTH WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ACBDENOTH
			(OID_, VERSION_, BDPERCENTAGE, BDBETA, F_OID__PASSE_CDF1A, F_PFINANCIAL_815C0, ID_ELAB)
			SELECT OID_, VERSION_, BDPERCENTAGE, BDBETA, F_OID__PASSE_CDF1A, F_PFINANCIAL_815C0, @ID_ELAB 
			FROM C6Staging.ACBDENOTH
		END

	--XXXXXXXXXXXXXXXXXXXXXXXX ACBDENTRY XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ACBDENTRY')
		BEGIN
			DELETE FROM C6Storico.ST_ACBDENTRY WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.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 OID_, VERSION_, F_PINDEXBREA_08079, F_PINSTRUMEN_FBCDE, BDPERCENTAGE, BDBETA, F_OID__PASSE_B8A1E, F_PFINANCIAL_65EBB, F_PFINANCIAL_C2FD3, @ID_ELAB 
			FROM C6Staging.ACBDENTRY
		END
 
	--XXXXXXXXXXXXXXXXXXXXXXXX ADBKRELAT XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ADBKRELAT')
		BEGIN	
			DELETE FROM C6Storico.ST_ADBKRELAT WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ADBKRELAT
			(OID_, VERSION_, TYPE__TYPE_CAB4A, ADBKRELDESCR, ADBKRELNOTE, DEPENDANT, PHONEDECIMAL, F_OID__ADBKR_CBA9A, F_PADDRESSBO_24DA1, ID_ELAB)
			SELECT OID_, VERSION_, TYPE__TYPE_CAB4A, ADBKRELDESCR, ADBKRELNOTE, DEPENDANT, PHONEDECIMAL, F_OID__ADBKR_CBA9A, F_PADDRESSBO_24DA1, @ID_ELAB 
			FROM C6Staging.ADBKRELAT
 		END

	--XXXXXXXXXXXXXXXXXXXXXXXX ADS4WS_PROMOTORI XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ADS4WS_PROMOTORI')
		BEGIN
			DELETE FROM C6Storico.ST_ADS4WS_PROMOTORI WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ADS4WS_PROMOTORI
			(Rete, Codice, Cognome, Nome, Prefisso, Firma, DataInizioCollaborazione, DataFineCollaborazione, DataUltimaAggiornamento, CAP, Citta, CittaNascita, DataNascita, Indirizzo, Provincia, Telefono, TelefonoAgenzia1, TelefonoAgenzia2, ID_ELAB)
			SELECT Rete, Codice, Cognome, Nome, Prefisso, Firma, DataInizioCollaborazione, DataFineCollaborazione, DataUltimaAggiornamento, CAP, Citta, CittaNascita, DataNascita, Indirizzo, Provincia, Telefono, TelefonoAgenzia1, TelefonoAgenzia2, @ID_ELAB 
			FROM C6Staging.ADS4WS_PROMOTORI
 		END
 
	--XXXXXXXXXXXXXXXXXXXXXXXX ALL_ASSET_CLASS_PER_PROMET XXXXXXXXXXXXXXXXXXXXXXXX--
	IF(@NomeTabella IS NULL OR @NomeTabella = 'ALL_ASSET_CLASS_PER_PROMET')
		BEGIN
			DELETE FROM C6Storico.ST_ALL_ASSET_CLASS_PER_PROMET WHERE ID_ELAB >= @ID_ELAB OR ID_ELAB < @MIN_ID_ELAB
			INSERT INTO C6Storico.ST_ALL_ASSET_CLASS_PER_PROMET
			(CODICE_ASSET_CLASS_INGRESSO, CODICE_ISIN, CODICE_MAF, CODICE_ADEGUATEZZA, ID_ELAB)
			SELECT CODICE_ASSET_CLASS_INGRESSO, CODICE_ISIN, CODICE_MAF, CODICE_ADEGUATEZZA, @ID_ELAB 
			FROM C6Staging.ALL_ASSET_CLASS_PER_PROMET
 		END
  
	--XXXXXXXXXXXXXXXXXXX
