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

CREATE FUNCTION [dbo].[Order_Ilac_listesi] (@TedaviTableId as Integer)  
RETURNS nvarchar(1000) AS  
BEGIN 
declare @IlacListe as nvarchar(max),	
	@IlacAdi as NVARCHAR(100)
SET @IlacListe = '' 
DECLARE Order_Ilac_Cursor CURSOR
   FOR SELECT ITEMADI FROM HTEDAVITABELADETAY HTD(NOLOCK) INNER JOIN TITEM T(NOLOCK) ON T.ITEMID=HTD.ITEMID WHERE ISNULL(KONSISTEMIZORUNLU,0)=1 AND  PASIF=0 AND HTEDAVITABELAID = @TedaviTableId
OPEN Order_Ilac_Cursor
FETCH NEXT FROM Order_Ilac_Cursor INTO  @IlacAdi
WHILE @@FETCH_STATUS = 0
BEGIN
	SET @IlacListe = @IlacListe +  @IlacAdi+ ', '
	FETCH NEXT FROM Order_Ilac_Cursor INTO  @IlacAdi
END
	IF CHARINDEX(',', ISNULL(@IlacListe,' ')) <> 0
		SET @IlacListe = LEFT(@IlacListe, LEN(@IlacListe)-1)
return @IlacListe
END
GO


