为简单起见,我有以下示例:
;WITH myCTE (CustID, Co) AS
(
SELECT CustomerID, CompanyName FROM Customers
)
SELECT CompanyName FROM Customers WHERE CustomerID = 'ALFKI'
union all --this does not work with two With statements
;WITH myCTE2 (CustID, Co) AS
(
SELECT CustomerID, CompanyName FROM Customers
)
SELECT CompanyName FROM Customers WHERE CustomerID = 'BBICT'
答案 0 :(得分:2)
;WITH myCTE (CustID, Co) AS
(
SELECT CustomerID, CompanyName FROM Customers
), myCTE2 (CustID, Co) AS
(
SELECT CustomerID, CompanyName FROM Customers
)
SELECT Co FROM myCTE WHERE CustID = 'BBICT'
union all
SELECT Co FROM myCTE2 WHERE CustID = 'ALFKI'
编辑:
假设OP选择了一个糟糕的例子。
否则,所需要的只是
SELECT CompanyName FROM Customers WHERE CustomerID IN ('ALFKI', 'BBICT' )
答案 1 :(得分:0)
您只需要将两个查询分开:
;WITH myCTE (CustID, Co) AS
(
SELECT CustomerID, CompanyName FROM Customers
)
,
myCTE2 (CustID, Co) AS
(
SELECT CustomerID, CompanyName FROM Customers
)
SELECT CompanyName FROM Customers WHERE CustomerID = 'BBICT'
UNION
SELECT CompanyName FROM Customers WHERE CustomerID = 'ALFKI'