zoukankan      html  css  js  c++  java
  • SQL NOTCTE

    CTE
    
    CTE(Common Table Expression),可以看做临时结果集,被后面的语句多次访问。
    使用CTE来简化代码复杂度。
    
    1.CTE可以自己引用自己,从而实现递归调用,
    2.在WITH 语句中可以定义多个临时结果集
    3.使用嵌套CTE时,需要使用UNION ALL(union不可以)来完成嵌套,使用MAXRECURSION来限制嵌套循环次数
    
    
    CREATE TABLE dbo.Categories
    (
        CategoryId INT IDENTITY(1,1) PRIMARY KEY,
        CategoryName NVARCHAR(200),
        ParentCategoryId INT DEFAULT NULL
    )
    
    CREATE TABLE dbo.Articles
    (
        ArticleId INT IDENTITY(1,1) PRIMARY KEY,
        ArticleTitle NVARCHAR(200) NOT NULL,
        CategoryId INT NOT NULL
    )
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C1',NULL
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C2',1
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C3',1
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C4',1
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C5',2
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C6',2
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C7',4
    
    INSERT INTO dbo.Categories(CategoryName,ParentCategoryId)
    SELECT 'C8',5
    
    INSERT INTO dbo.Articles(ArticleTitle,CategoryId)
    SELECT 'A1',8
    
    --==================================================================================================
    --普通CTE
    WITH T1 AS
    (
        SELECT CategoryId,CategoryName,ParentCategoryId FROM dbo.Categories
        WHERE ParentCategoryId IS NOT NULL
    )
    SELECT * FROM T1;
    --==================================================================================================
    --递归CTE
    WITH T2 AS
    (
        SELECT C1.CategoryId,C1.CategoryName,C1.ParentCategoryId,0 AS CategoryLevel FROM dbo.Categories C1
        WHERE ParentCategoryId IS NULL
        UNION ALL
        SELECT C2.CategoryId,C2.CategoryName,C2.ParentCategoryId,T2.CategoryLevel+1 AS CategoryLevel FROM dbo.Categories C2
        INNER JOIN T2 ON T2.CategoryId=C2.ParentCategoryId
    )
    SELECT * FROM T2;
    --==================================================================================================
    --WITH语句中包含多个CTE
    WITH T2 AS
    (
        SELECT C1.CategoryId,C1.CategoryName,C1.ParentCategoryId,0 AS CategoryLevel FROM dbo.Categories C1
        WHERE ParentCategoryId IS NULL
        UNION ALL
        SELECT C2.CategoryId,C2.CategoryName,C2.ParentCategoryId,T2.CategoryLevel+1 AS CategoryLevel FROM dbo.Categories C2
        INNER JOIN T2 ON T2.CategoryId=C2.ParentCategoryId
    ),
    T3 AS 
    (
        SELECT ArticleId,ArticleTitle,CategoryId FROM dbo.Articles
    )
    SELECT T3.ArticleId,T3.ArticleTitle,T2.CategoryId,T2.CategoryName,T2.CategoryLevel
    FROM T2 INNER JOIN T3 
    ON T2.CategoryId=T3.CategoryId;
    
    
    --==================================================================================================
    --使用MAXRECURSION来限制嵌套循环层级,当超过指定层级时,会报
    --Msg 530, Level 16, State 1, Line 2
    --The statement terminated. The maximum recursion 2 has been exhausted before statement completion.
    
    WITH T2 AS
    (
        SELECT C1.CategoryId,C1.CategoryName,C1.ParentCategoryId,0 AS CategoryLevel FROM dbo.Categories C1
        WHERE ParentCategoryId IS NULL
        UNION ALL
        SELECT C2.CategoryId,C2.CategoryName,C2.ParentCategoryId,T2.CategoryLevel+1 AS CategoryLevel FROM dbo.Categories C2
        INNER JOIN T2 ON T2.CategoryId=C2.ParentCategoryId
    )
    SELECT * FROM T2
    OPTION(MAXRECURSION 2)
  • 相关阅读:
    MySQL插入数据慢解决办法
    java的 System.currentTimeMillis() 与时区
    单元测试断言系统 类似于 生活中如何说话
    IDEA、Eclipse 默认常用快捷键对比总结,visual studio快捷键总结
    使用过的正则匹配的例子
    filters的this问题
    vuex的使用
    offset/scroll/clien系列知识点
    promise对象的理解
    语义化标签的理解
  • 原文地址:https://www.cnblogs.com/TeyGao/p/2727489.html
Copyright © 2011-2022 走看看