SqlServer uses a common table expression (CTE) to build an infinite tree structure. sqlservercte

Source: Internet
Author: User

SqlServer uses a common table expression (CTE) to build an infinite tree structure. sqlservercte

Starting from SQL Server 2005, we can use CTE to support recursive queries. CTE is the common table expression.

A common table expression (CTE) is a temporary naming result set defined in the query and will be used in the from clause. Each CTE is defined only once (but can be referenced any time in its scope) and will survive during the query period. You can use CTE to perform recursive operations.

DECLARE @Level INT=3;WITH cte_parent(CategoryID,CategoryName,ParentCategoryID,Level)AS(  SELECT category_id,category_name,parent_category_id,1 AS Level  FROM TianShenLogistic.dbo.ProductCategory WITH(NOLOCK) WHERE category_id IN ( SELECT category_id  FROM TianShenLogistic.dbo.ProductCategory  WHERE parent_category_id=0 )  UNION ALL  SELECT b.category_id,b.category_name,b.parent_category_id,a.Level+1 AS Level  FROM TianShenLogistic.dbo.ProductCategory b  INNER JOIN cte_parent a  ON a.CategoryID = b.parent_category_id)SELECT  CategoryID AS value, CategoryName as label, ParentCategoryID As parentId, LevelFROM cte_parent WHERE Level <=@Level;public static List<LogisticsCategoryTreeEntity> GetLogisticsCategoryByParent(int? level)    {      if (level < 1) return null;      var dataResult = CategoryDA.GetLogisticsCategoryByParent(level);      var firstlevel = dataResult.Where(d => d.level == 1).ToList();      BuildCategory(dataResult, firstlevel);      return firstlevel;    }    private static void BuildCategory(List<LogisticsCategoryTreeEntity> allCategoryList, List<LogisticsCategoryTreeEntity> categoryList)    {      foreach (var category in categoryList)      {        var subCategoryList = allCategoryList.Where(c => c.parentId == category.value).ToList();        if (subCategoryList.Count > 0)        {          if (category.children == null) category.children = new List<LogisticsCategoryTreeEntity>();          category.children.AddRange(subCategoryList);          BuildCategory(allCategoryList, category.children);        }      }    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.