多个子查询

时间:2013-06-25 15:10:59

标签: sql-server-2008 subquery

我可以在一个查询中使用多个WITH子查询吗?

;WITH x AS (
  SELECT ...
),
WITH y AS (
  SELECT ...
)
SELECT * FROM z 
INNER JOIN x ON ...
INNER JOIN y ON ...

1 个答案:

答案 0 :(得分:3)

它被称为common table expression,是的,您可以使用多个CTE,只使用一个WITH并删除第二个with,如下所示:

WITH x AS (
  SELECT ...
),y AS (
  SELECT ...
)
SELECT * FROM z 
INNER JOIN x ON ...
INNER JOIN y ON ...