Can I use multiple “with”?

前端 未结 2 1590
耶瑟儿~
耶瑟儿~ 2020-12-22 17:42

Just for example:

With DependencedIncidents AS
(
    SELECT INC.[RecTime],INC.[SQL] AS [str] FROM
    (
        SELECT A.[RecTime] As [RecTime],X.[SQL] As [S         


        
相关标签:
2条回答
  • 2020-12-22 18:04

    Yes - just do it this way:

    WITH DependencedIncidents AS
    (
      ....
    ),  
    lalala AS
    (
      ....
    )
    

    You don't need to repeat the WITH keyword

    0 讨论(0)
  • 2020-12-22 18:16

    Try:

    With DependencedIncidents AS
    (
        SELECT INC.[RecTime],INC.[SQL] AS [str] FROM
        (
            SELECT A.[RecTime] As [RecTime],X.[SQL] As [SQL] FROM [EventView] AS A 
            CROSS JOIN [Incident] AS X
                WHERE
                    patindex('%' + A.[Col] + '%', X.[SQL]) > 0
        ) AS INC
    ),
    lalala AS
    (
        SELECT INC.[RecTime],INC.[SQL] AS [str] FROM
        (
            SELECT A.[RecTime] As [RecTime],X.[SQL] As [SQL] FROM [EventView] AS A 
            CROSS JOIN [Incident] AS X
                WHERE
                    patindex('%' + A.[Col] + '%', X.[SQL]) > 0
        ) AS INC
    )
    

    And yes, you can reference common table expression inside common table expression definition. Even recursively. Which leads to some very neat tricks.

    0 讨论(0)
提交回复
热议问题