IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[HDOSYA_ILAC_LISTE]') AND xtype in (N'FN', N'IF', N'TF'))
DROP FUNCTION [dbo].HDOSYA_ILAC_LISTE
GO

CREATE FUNCTION [dbo].[HDOSYA_ILAC_LISTE] (@HDosyaId as Integer, @GIRISTARIH AS DATETIME)  
RETURNS nvarchar(4000) AS  
BEGIN 
declare @FLISTE as nvarchar(4000),
	@FATURA as nvarchar(150)
SET @FLISTE = '' 
DECLARE HDOSYA_FATURA_CURSOR CURSOR
   FOR SELECT ITEMADI FROM HESAPISL (NOLOCK) LEFT OUTER JOIN TITEM (NOLOCK) ON HESAPISL.ITEMID=TITEM.ITEMID 
   WHERE HDOSYAID = @HDosyaId AND DBO.TARIHAL(ITARIH) = @GIRISTARIH AND IPTAL=0 AND TITEM.ITEMTURUID=3
   GROUP BY ITEMADI
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)
set @FLISTE=CHAR(13)+'> '+@FLISTE
return @FLISTE
END
GO