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)
  • 相关阅读:
    Java设计模式—模板方法模式
    STM32 常用GPIO操作函数记录
    GPIO 配置之ODR, BSRR, BRR 详解
    STM32F4先设置寄存器还是先使能时钟
    LDR指令的格式:
    printf函数重定向
    stm32F4各个库文件的作用分析
    STM32F4时钟设置分析
    STM32F407存储器和总线架构
    SPI移位寄存器
  • 原文地址:https://www.cnblogs.com/TeyGao/p/2727489.html
Copyright © 2011-2022 走看看