CREATE procedure [C6StagingPeriodico].[ClienteProdottiFAI]
@Rete as varchar(1),
@CodiceFiscale as varchar(16)
as
declare @tblFlagNota table
(
	FlagFAI char(1),
	DataFAI varchar(10)
)
		--DECLARE @dtrim VARCHAR(8)
		--set @dtrim = c6martperiodico.gettrimestre(getDate(),0)
		--print @dtrim
		Declare @dtrimFAI varchar(10)
		set @dtrimFAI =  Convert(varchar(10), Cast(c6martperiodico.getPreviousTrimestre(getDate(),0) as Datetime),103)
		print @dtrimFAI
DECLARE @conta INT
SELECT @conta = count(*)
FROM [C6MartPeriodico].[PATRIMONIO_BF]
WHERE rtrim(ltrim(cod_interno))  IN (
		'IA',
		'IP',
		'IC',
		'ID',
		'PE'
			)
	AND cod_fiscale = @CodiceFiscale
	AND rete = @Rete
if @conta >= 1
begin 
	insert into @tblFlagNota (FlagFAI,DataFAI) Values ('S',@dtrimFAI)
end
else
	insert into @tblFlagNota (FlagFAI,DataFAI) Values ('N',@dtrimFAI)
Select FlagFAI,DataFAI from @tblFlagNota