我可以使用多个“与”?

只是例如

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 ) 

不起作用。 “错误附近”。

我也想先用里面第二个用。 是真的还是我需要使用临时表?

尝试:

 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 ) 

是的,您可以在公用表expression式定义中引用公用表expression式。 即使是recursion的。 这导致一些非常干净的把戏 。

是的 – 就这样做:

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

您不需要重复WITH关键字