web-dev-qa-db-fra.com

Puis-je utiliser plusieurs "avec"?

juste par exemple

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
)

With 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
)

ne fonctionne pas. "Erreur près de".

Aussi, je veux utiliser le premier avec le second intérieur avec. Est-ce réel ou dois-je utiliser des tables temporaires?

159
cnd

Essayer:

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
)

Et oui, vous pouvez référencer l'expression de table commune dans la définition de l'expression de table commune. Même récursivement. Ce qui conduit à des très astuces soignées .

276
Tomek Szpakowicz

Oui, faites-le simplement comme ça:

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

Vous n'avez pas besoin de répéter le mot clé WITH

99
marc_s