if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetOSRTParentName]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetOSRTParentName]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE FUNCTION [dbo].[GetOSRTParentName] (@o_srt_id int,@region_cod int,@displayitself bit=1)  
RETURNS nvarchar(2000) AS  
BEGIN 
	DECLARE @NAMES nvarchar(2000)
	DECLARE @tempname nvarchar(250)
	DECLARE temp_cur CURSOR LOCAL FORWARD_ONLY--SCROLL  
		FOR 
		select	RTRIM(LTRIM(ISNULL(O_SRT.[NAME],'?'))) 
		from  GETOSRTPARENTSET(@o_srt_id,@region_cod,@displayitself) GETOSRTPARENTSET INNER JOIN
                      	O_SRT ON GETOSRTPARENTSET.o_srt_id = O_SRT.O_SRT_ID and GETOSRTPARENTSET.REGION_COD = O_SRT.REGION_COD 
	OPEN temp_cur
	--FETCH FIRST  FROM temp_cur INTO @tempname
	FETCH NEXT  FROM temp_cur INTO @tempname
	SET @NAMES = ''
	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF (@NAMES = '')
			SET @NAMES = @tempname
		ELSE
			SET @NAMES = @NAMES +'; '+@tempname
		FETCH NEXT FROM  temp_cur INTO  @tempname
	END
	CLOSE  temp_cur
	DEALLOCATE  temp_cur
	
	RETURN @NAMES 
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT  EXECUTE  ON [dbo].[GetOSRTParentName]  TO [public]
GO

