IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UFD_BUSCA_CONTA_BC_CONTAS]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	DROP FUNCTION [dbo].[UFD_BUSCA_CONTA_BC_CONTAS]
GO 

CREATE FUNCTION [dbo].[UFD_BUSCA_CONTA_BC_CONTAS](	@CD_EMP 		INT,
														@CD_CNT 		INT)
RETURNS @RS_RETURN TABLE(
		CD_CTB_CNT_RED	INT, 
		CD_CTB_CNT		VARCHAR(50), 
		DS_CNT			VARCHAR(100),
		NR_CNT			VARCHAR(50),
		TP_CNT			INT,
		TITULAR_CNT		VARCHAR(200),
		CD_FILIAL_CNT 	INT,
		NR_CNT_EDI 		VARCHAR(100),
		CD_BC			INT)

	BEGIN
		
		INSERT INTO @RS_RETURN
			SELECT 
				ISNULL(CD_CTB_CNT_RED,0) AS CD_CTB_CNT_RED,
				ISNULL(CD_CTB_CNT,'')    AS CD_CTB_CNT,
				DS_CNT			,
				NR_CNT			,
				TP_CNT			,
				TITULAR,
				CD_FILIAL_CNT,
				NR_CNT_EDI + '-' + DV_NR_CNT_EDI AS NR_CNT_EDI,
				CD_BC
			FROM BC_CONTAS
			WHERE BC_CONTAS.CD_EMP 		= @CD_EMP
				  AND BC_CONTAS.CD_CNT 	= @CD_CNT			
		
		RETURN
		
	END
	
GO


