SQL Server 没有类似于OracleSTART WITH NAME='xx'CONNECT BY PRIOR ID=PARENT_ID这样的语句,但是可以通过自定义标准函数+With语句实现,速度也是杠杠的
ALTER FUNCTION [dbo].[RecursionSysLocation]( --Add the parameters for the function here @ParentId nvarchar(36) ) RETURNS TABLE AS RETURN( with temp ( [Id], [parentid]) as( selectId, ParentId fromSysLocation where ParentId = @ParentId union all selecta.Id, a.ParentId fromSysLocation a inner join temp on a.ParentId = temp.[Id]) select s.Id, s.ParentId from SysLocation s where Id=@ParentId union all select * from temp)