CREATE FUNCTION F_NRTOWNObj ( @obj NVARCHAR(500) )
RETURNS NVARCHAR(2000)
AS
BEGIN
DECLARE @str NVARCHAR(200)
DECLARE @num INT
DECLARE @next INT
DECLARE @length INT
SET @next = 1
SET @length = 0
SET @str = ''
SELECT @length = dbo.Get_StrArrayLength(@obj, ',')
WHILE @next <= @length
BEGIN
SELECT @num = dbo.Get_StrArrayStrOfIndex(@obj, ',', @next)
SELECT @str+= T.cname + '(' + p.cname + ')' + ','
FROM CAMSDB.dbo.TOWN t
INNER JOIN CAMSDB.dbo.PROVINCE p ON t.PROVINCE_ID = p.PROVINCE_ID
WHERE t.TOWN_ID = @num
SET @next += 1
END
RETURN @str
END
go