父子集 拿到父级的parentName
差了个order by
子集拿到每个父级的ID 再次需要一个方法
CREATE FUNCTION [dbo].[GetOrgParentName] ( @s VARCHAR(max) , --待分拆的字符串 @split VARCHAR(10) --数据分隔符 ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @splitlen INT DECLARE @re TABLE ( id INT,col VARCHAR(100) ) SET @splitlen = LEN(@split + 'a') - 2 DECLARE @index INT DECLARE @result NVARCHAR(max) SET @index=1 WHILE CHARINDEX(@split, @s) > 0 BEGIN INSERT @re VALUES (@index, LEFT(@s, CHARINDEX(@split, @s) - 1) ) SET @s = STUFF(@s, 1, CHARINDEX(@split, @s) + @splitlen, '') SET @index=@index+1 END INSERT @re VALUES (@index, @s ) DECLARE @cnt int SELECT @cnt=COUNT(*) FROM @re SELECT @result=col FROM @re WHERE id=@cnt-1 RETURN @result END GO