美文网首页
SQL Server WITH AS使用

SQL Server WITH AS使用

作者: YANG_LIVE | 来源:发表于2020-09-21 16:38 被阅读0次

WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ALL的不同部分,作为提供数据的部分。

WITH    cr
          AS ( SELECT   Card_Level ,
                        Card_Level_Desc
               FROM     dbo.tbCRM_Card_Level
               WHERE    Card_Level IN ( 1, 2 )
             )
    SELECT  *
    FROM    tbCRM_Card_Level
    WHERE   Card_Level IN ( SELECT  Card_Level
                            FROM    cr );

WITH    cte1
          AS ( SELECT   *
               FROM     tbCRM_Card_Level
               WHERE    Card_Level = 1
             ),
        cte2
          AS ( SELECT   *
               FROM     tbCRM_Card_Level
               WHERE    Card_Level = 2
             ),
        cte3
          AS ( SELECT   *
               FROM     tbCRM_Card_Level
               WHERE    Card_Level = 3
             )
    SELECT  a.*
    FROM    tbCRM_Card_Level a ,
            cte2 b ,
            cte3 c
    WHERE   a.Card_Level = b.Card_Level
            AND a.Card_Level = c.Card_Level ;

相关文章

网友评论

      本文标题:SQL Server WITH AS使用

      本文链接:https://www.haomeiwen.com/subject/mcnvyktx.html