IF OBJECT_ID('UFD_BUSCA_PARAM_SISTEMA') IS NOT NULL
	BEGIN 
		DROP FUNCTION UFD_BUSCA_PARAM_SISTEMA
	END
	
GO	

 
/****** Object:  UserDefinedFunction [dbo].[UFD_BUSCA_PARAM_SISTEMA]    Script Date: 07/28/2013 22:26:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE   FUNCTION [dbo].[UFD_BUSCA_PARAM_SISTEMA](	    @CD_EMP 		INT,
														@CD_CHAVE 		VARCHAR(100),
														@TP_CHAVE		INT,
														@CD_FILIAL 		INT)
RETURNS VARCHAR(100) AS
	BEGIN
		DECLARE @VALOR VARCHAR(100)
		
		-- 0 - Chave empresa
		-- 1 - Chave filial
			
		IF @TP_CHAVE = 0 
			BEGIN
				SET @VALOR = CAST((SELECT TOP 1 VALOR FROM PRC_EMP_CONFIG WHERE CD_EMP = @CD_EMP AND CD_CHAVE = @CD_CHAVE) AS VARCHAR)
			END
		ELSE
			BEGIN
				SET @VALOR = CAST((SELECT TOP 1 VALOR FROM PRC_FILIAL_CONFIG WHERE CD_EMP = @CD_EMP AND CD_FILIAL = @CD_FILIAL AND CD_CHAVE = @CD_CHAVE) AS VARCHAR)
			END
				
		SET @VALOR = ISNULL(@VALOR,'')
		
		RETURN @VALOR
		
	END
	
GO


