CREATE FUNCTION dbo.fun_orgname(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @str varchar(8000)
SET @str = ''
select @str = @str + ',' +r.NAME from PRINCIPAL_ORG t left join ORG r on t.ORG_ID=r.ID where t.PRINCIPAL_ID=@id
RETURN STUFF(@str, 1, 1, '')
END
GO