SQLSERVER的遞歸

        實際項目如遇到按照如地區這樣樹狀結構的條件遞歸查詢數據的場景中,可使用。url

CREATE FUNCTION [dbo].[GetAllChildrenTypeById]
(
    @id varchar(50)
)
RETURNS TABLE 

AS


RETURN 
with cte as
(
 SELECT [id]
      ,[dictionaryTypeCode]
      ,[text]
      ,[parentId]
      ,[sort]
      ,[availabl]
      ,[leaf]
      ,[url]
      ,[value] FROM [Dictionary] where ID = @id
union all
SELECT a.[id],a.[dictionaryTypeCode],a.[text],a.[parentId],a.[sort],a.[availabl],a.[leaf],a.[url],a.[value] FROM [Dictionary] a  join cte b on a.[parentId] = b.Id
)
select id from cte 


GO
相關文章
相關標籤/搜索