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

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

CREATE FUNCTION [dbo].[HDOSYA_HFATURA_LISTE] (@HDosyaId as Integer)  
RETURNS nvarchar(150) AS  
BEGIN 
	declare 
	  @FLISTE as nvarchar(150),
	  @FATURA as nvarchar(20)

	SET @FLISTE = '' 

	DECLARE HDOSYA_FATURA_CURSOR CURSOR
	   FOR SELECT FATNO FROM HESAPISL INNER JOIN FATURA ON HESAPISL.OZELHFATISLID=FATURA.FATID WHERE HDOSYAID = @HDosyaId AND IPTAL=0 GROUP BY FATNO
	OPEN HDOSYA_FATURA_CURSOR
	FETCH NEXT FROM HDOSYA_FATURA_CURSOR INTO @FATURA
	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @FLISTE = @FLISTE + @FATURA + ', '
		FETCH NEXT FROM HDOSYA_FATURA_CURSOR INTO @FATURA
	END
		IF CHARINDEX(',', ISNULL(@FLISTE,' ')) <> 0
			SET @FLISTE = LEFT(@FLISTE, LEN(@FLISTE)-1)
	return @FLISTE
END
GO