CREATE FUNCTION [C6StagingPeriodico].[Info_DaProdottoCSaCatalogo_preASU2]
(
	@TipoProdotto varchar(5),
	@CodInterno varchar(13),
	@CodMAF varchar(13),
	@CodISIN varchar(16),
	@CodSottoprodotto varchar(13)
)
RETURNS varchar(63)
AS
BEGIN
	-- Declare the return variable here
		DECLARE @ResultVar  VARCHAR(63)
	-- Add the T-SQL statements to compute the return value here
	IF @TipoProdotto in ('FI', 'GE', 'ASUL', 'ASU1', 'ASVI', 'ASCM', 'AC','CC' )    -- TRINGALI inseriti CC
	SELECT TOP 1 @ResultVar = CATALOGUEID 
	FROM C6StagingPeriodico.CATITEMREF cat
	where 1=1 and cat.COD_INTERNO = (case rtrim(isnull(@CodInterno,'')) when '' then '$' else @CodInterno end) 
	and cat.COD_SOTTOPRODOTTO= (case rtrim(isnull(@CodSottoprodotto,'')) when '' then '$' else @CodSottoprodotto end)
	ORDER BY ISVISIBLE DESC
	IF @TipoProdotto in ('DT','ASD' ) 
	SELECT TOP 1 @ResultVar = CATALOGUEID 
	FROM C6StagingPeriodico.CATITEMREF cat
	where 1=1 and cat.COD_INTERNO = (case rtrim(isnull(@CodInterno,'')) when '' then '$' else @CodInterno end)
	ORDER BY ISVISIBLE DESC
	IF @TipoProdotto in ('GES' ) 
	SELECT TOP 1 @ResultVar = CATALOGUEID 
	FROM C6StagingPeriodico.CATITEMREF cat
	where 1=1 
	and cat.COD_INTERNO = (case rtrim(isnull(@CodInterno,'')) when '' then '$' else @CodInterno end) 
	and cat.COD_MAF=(case rtrim(isnull(@CodMAF,'')) when '' then '$' else @CodMAF end)
	ORDER BY ISVISIBLE DESC
	-- Return the result of the function
	RETURN @ResultVar
END