--C6MartPeriodico].[getDESPRODCAT]('1','1')
CREATE FUNCTION [C6MartPeriodico].[getDESPRODCAT_BCK] 
(
	@CODPROD as varchar(8),
	@SUBPROD as varchar(5) 
)
RETURNS varchar(128)
AS
BEGIN
DECLARE @DESPROD as varchar(128)
DECLARE @conta as smallint 
SET @DESPROD = ''
SELECT @conta = count(cataloguename) FROM c6StagingPeriodico.catitemref
WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @SUBPROD AND ISVISIBLE = 1   
IF @conta = 1       
		SELECT  @DESPROD  = cataloguename   FROM c6StagingPeriodico.catitemref
		WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @SUBPROD AND ISVISIBLE = 1  
ELSE
	BEGIN
		SELECT @conta = count(cataloguename) FROM c6StagingPeriodico.catitemref
		WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @CODPROD AND ISVISIBLE = 1   
		IF @conta = 1       
			SELECT  @DESPROD  = cataloguename   FROM c6StagingPeriodico.catitemref
			WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @CODPROD AND ISVISIBLE = 1 
			ELSE
		BEGIN 
			SELECT @conta = count(cataloguename) FROM c6StagingPeriodico.catitemref
			WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @SUBPROD
			IF @conta = 1       
				SELECT  @DESPROD  = cataloguename   FROM c6StagingPeriodico.catitemref
				WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @SUBPROD  
			ELSE
				BEGIN
					SELECT @conta = count(cataloguename) FROM c6StagingPeriodico.catitemref
					WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @CODPROD     
					IF @conta = 1       
						SELECT  @DESPROD  = cataloguename   FROM c6StagingPeriodico.catitemref
						WHERE COD_INTERNO = @CODPROD AND COD_SOTTOPRODOTTO = @CODPROD   
				END  
		END 
	END  
RETURN @DESPROD
END