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

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE   FUNCTION [dbo].[UFD_BUSCA_CONTA_RC_CLI] (@CD_EMP 		INT, 
												  @CD_CLI 		INT,
												  @TP_CNT_CTB	INT)
RETURNS @RS_RETURN TABLE(		
		CD_CTB_CNT					VARCHAR(50),
		CD_CTB_CNT_RED				INT,
		RZ_CLI						VARCHAR(200))
		
	BEGIN

		INSERT INTO @RS_RETURN
			SELECT 			    
			    ISNULL(RC_CLI_CNT_CTB.CD_CTB_CNT,''), 
				ISNULL(RC_CLI_CNT_CTB.CD_CTB_CNT_RED,0), 
			    RC_CLI.RZ_CLI    
		    FROM 
				RC_CLI 
				LEFT OUTER JOIN RC_CLI_CNT_CTB ON 
				    RC_CLI.CD_EMP = RC_CLI_CNT_CTB.CD_EMP AND 
				    RC_CLI.CD_CLI = RC_CLI_CNT_CTB.CD_CLI    
		    WHERE 
				RC_CLI.CD_EMP 					= @CD_EMP
				AND RC_CLI.CD_CLI   			= @CD_CLI
				AND RC_CLI_CNT_CTB.TP_CNT_CTB 	= @TP_CNT_CTB
		
		RETURN
	END		
	
GO


