CREATE procedure [C6Staging].[RP_ST_TB_ESG_ANAG_STRUM] as
begin 
	-- Log esecuzione
	INSERT INTO LOG_ESECUZIONE
		(Nome, Inizio, Nota)
	VALUES (
		'RP_ST_TB_ESG_ANAG_STRUM',
		GETDATE(),
		'Elaborazione giornaliera'
	)
truncate table C6Staging.RP_TB_ESG_ANAG_STRUM
INSERT INTO [C6Staging].[RP_TB_ESG_ANAG_STRUM]
           ([ChiaveProdotto]
      ,[CodSottoprodotto]
      ,[CodInterno]
      ,[CodMaf]
      ,[CodAdeguatezza]
      ,[CodIsin]
      ,[ESG_E]
      ,[ESG_S]
      ,[ESG_G]
      ,[CatSFDR]
      ,[ESGRating]
      ,[TrendESGRating]
	  ,[TrendESGRating_Val]
      ,[dtInserimento]
      ,[dtAggiornamento]
      ,[UserIns]
	  ,[CLIENT_ID]
      ,[C_TIPO_STRUMENTO]
      ,[LABEL_Green_Bond]
      ,[LABEL_Social_Bond]
      ,[LABEL_Sustainability_Bond]
      ,[LABEL_Transition_Bond]
      ,[LABEL_Sust_linked_bond]
      ,[LABEL_EU_GREEN_BOND_STANDARD]
      ,[SDG_06_NET_ALIGNMENT_SCORE]
      ,[SDG_07_NET_ALIGNMENT_SCORE]
      ,[SDG_12_NET_ALIGNMENT_SCORE]
      ,[SDG_13_NET_ALIGNMENT_SCORE]
      ,[SDG_14_NET_ALIGNMENT_SCORE]
      ,[SDG_15_NET_ALIGNMENT_SCORE]
      ,[SDG_01_NET_ALIGNMENT_SCORE]
      ,[SDG_02_NET_ALIGNMENT_SCORE]
      ,[SDG_03_NET_ALIGNMENT_SCORE]
      ,[SDG_04_NET_ALIGNMENT_SCORE]
      ,[SDG_05_NET_ALIGNMENT_SCORE]
      ,[SDG_08_NET_ALIGNMENT_SCORE]
      ,[SDG_09_NET_ALIGNMENT_SCORE]
      ,[SDG_10_NET_ALIGNMENT_SCORE]
      ,[SDG_11_NET_ALIGNMENT_SCORE]
      ,[SDG_16_NET_ALIGNMENT_SCORE]
      ,[SDG_17_NET_ALIGNMENT_SCORE]
      ,[PERC_INVESTIMENTO_TAXONOMY]
      ,[PERC_INVESTIMENTO_SFDR]
      ,[PERC_INVEST_OTHER_E_NO_TAX]
      ,[PERC_INVEST_SFDR_MIN_SUST_E]
      ,[PERC_INVEST_SFDR_MIN_SUST_S]
      ,[PAI_LAB_A_Emissioni_Gas_Serra]
      ,[PAI_LAB_A_Biodiversita]
      ,[PAI_LAB_A_Acqua]
      ,[PAI_LAB_A_Rifiuti]
      ,[PAI_LAB_A_Gov_Ambientale]
      ,[PAI_LAB_A_Comb_Fossili]
      ,[PAI_LAB_A_Eff_energetica]
	  ,[PAI_LAB_A_Settore_Immobiliare]
      ,[PAI_LAB_S_Questioni_Soc_Dip]
      ,[PAI_LAB_S_Gov_Sociali]
      ,[U_INFO_ESG]
      ,[AGGREG_PAI_LAB_A]
      ,[AGGREG_PAI_LAB_S]
      ,[AGGREG_BOND]
      ,[REF_DATE])
   	select  [ChiaveProdotto]
      ,[CodSottoprodotto]
      ,[CodInterno]
      ,[CodMaf]
      ,[CodAdeguatezza]
      ,[CodIsin]
      ,case when [ESG_E]='0' then 'N' else ESG_E end
      ,[ESG_S]
      ,[ESG_G]
      ,[CatSFDR]
      ,[ESGRating]
      ,[TrendESGRating]
	  ,[TrendESGRating_Val]
      ,[dtInserimento]
      ,[dtAggiornamento]
      ,[UserIns]
	  ,[CLIENT_ID]
      ,[C_TIPO_STRUMENTO]
      ,[LABEL_Green_Bond]
      ,[LABEL_Social_Bond]
      ,[LABEL_Sustainability_Bond]
      ,[LABEL_Transition_Bond]
      ,[LABEL_Sust_linked_bond]
      ,[LABEL_EU_GREEN_BOND_STANDARD]
      ,[SDG_06_NET_ALIGNMENT_SCORE]
      ,[SDG_07_NET_ALIGNMENT_SCORE]
      ,[SDG_12_NET_ALIGNMENT_SCORE]
      ,[SDG_13_NET_ALIGNMENT_SCORE]
      ,[SDG_14_NET_ALIGNMENT_SCORE]
      ,[SDG_15_NET_ALIGNMENT_SCORE]
      ,[SDG_01_NET_ALIGNMENT_SCORE]
      ,[SDG_02_NET_ALIGNMENT_SCORE]
      ,[SDG_03_NET_ALIGNMENT_SCORE]
      ,[SDG_04_NET_ALIGNMENT_SCORE]
      ,[SDG_05_NET_ALIGNMENT_SCORE]
      ,[SDG_08_NET_ALIGNMENT_SCORE]
      ,[SDG_09_NET_ALIGNMENT_SCORE]
      ,[SDG_10_NET_ALIGNMENT_SCORE]
      ,[SDG_11_NET_ALIGNMENT_SCORE]
      ,[SDG_16_NET_ALIGNMENT_SCORE]
      ,[SDG_17_NET_ALIGNMENT_SCORE]
      ,[PERC_INVESTIMENTO_TAXONOMY]
      ,[PERC_INVESTIMENTO_SFDR]
      ,[PERC_INVEST_OTHER_E_NO_TAX]
      ,[PERC_INVEST_SFDR_MIN_SUST_E]
      ,[PERC_INVEST_SFDR_MIN_SUST_S]
      ,[PAI_LAB_A_Emissioni_Gas_Serra]
      ,[PAI_LAB_A_Biodiversita]
      ,[PAI_LAB_A_Acqua]
      ,[PAI_LAB_A_Rifiuti]
      ,[PAI_LAB_A_Gov_Ambientale]
      ,[PAI_LAB_A_Comb_Fossili]
      ,[PAI_LAB_A_Eff_energetica]
	   ,[PAI_LAB_A_Settore_Immobiliare]
      ,[PAI_LAB_S_Questioni_Soc_Dip]
      ,[PAI_LAB_S_Gov_Sociali]
      ,[U_INFO_ESG]
      ,[AGGREG_PAI_LAB_A]
      ,[AGGREG_PAI_LAB_S]
      ,[AGGREG_BOND]
      ,[REF_DATE]
	from [CONSUNI].CatalogoProdotti.dbo.TB_ESG_ANAG_STRUM
	-- Log esecuzione
	UPDATE
		LOG_ESECUZIONE
	SET
		Fine = GETDATE(),
		Tipo = 'INSERT',
		Righe = @@ROWCOUNT
	WHERE
		Nome = 'RP_ST_TB_ESG_ANAG_STRUM' AND
		Inizio = (
			SELECT
				MAX(Inizio)
			FROM
				LOG_ESECUZIONE
			WHERE
				Nome = 'RP_ST_TB_ESG_ANAG_STRUM'
		)
end