CREATE     procedure [C6MartPeriodico].[PL_FD190PatrimonioFinanziario_PTFRibilanciato] --'F','VGNMRA68C64F205S'
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
SELECT [Banca]
      ,[Ordinamento]
      ,[AssetClassId]
      ,[AssetClassName]
	  ,chiaveptfmod
      ,[Controvalore]
      ,[Percentuale]
      ,[Totale]
	  ,percentuale_modello
	  ,var_prog
	  into #ptf
 FROM [C6StampeCentralizzate].[C6MartPeriodico].[tb_fd190PatrimonioFinanziario_PTFRibilanciato]
  WHERE	[Rete] 		= @Rete 
  AND	[CodiceFiscale] = @CodiceFiscale
  order by ordinamento
  --aggiunta
declare @mia_chiaveptfmod int
SELECT
	  @mia_chiaveptfmod=ChiavePtfMod
  FROM [C6StampeCentralizzate].[C6MartPeriodico].[tb_fd190PatrimonioFinanziario_PTFRibilanciato]
  WHERE	[Rete] 		= @Rete 
  AND	[CodiceFiscale] = @CodiceFiscale
  order by ordinamento
  select * 
  into #ptf_mod
  from C6StagingPeriodico.RP_DistribuzionePtfModello_Tattici_MacroAssetClass where chiavePtfMod=@mia_chiaveptfmod
  select Banca,Ordinamento,AssetClassId,AssetClassName,ChiavePtfMod,Controvalore,Percentuale,Totale,Percentuale_Modello*100 as percentuale_modello,var_prog as var_prog  
  into #tot
  from #ptf
  union all
  select 'Fideuram',
  ordinamento,
  asset,
  descrizione,
  chiavePtfMod,
  NULL,
  0.00000,
  NULL,
  percentuale*100,
  NULL as var_prog from #ptf_mod where asset not in (select ASSETCLASSID from #ptf) 
  order by Ordinamento
	DECLARE @Totale [decimal](19, 5);
	DECLARE @var_prog [decimal](19, 5);
	SELECT @Totale = Totale, @var_prog = var_prog
FROM #ptf;
  update #tot 
  set Totale=@Totale, var_prog=@var_prog
  select * from #tot
END