• 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)
  • 相关阅读:
    ComboBox联动DataGridview之Access表名联动表内容
    通过进程ID获取窗口句柄(微软)
    Excel操作类
    C# TextBox自动完成
    Mdi悬浮子窗体不超过主窗体边界
    Excel导入数据库_Excel2Sql
    C#连接SQL的增删改查
    用dynamic expression运行时访问dynamic object
    添加模式匹配
    对帐脚本语言的修改
  • 原文地址:https://www.cnblogs.com/TeyGao/p/2727489.html
Copyright © 2020-2023  润新知