我需要做这样的事情,但SQL Server 2008不喜欢它.我的查询实际上比这更复杂,我意识到这不是完成我正在做的事情的最好方法,但我的重点是WITH语句的功能而不是select和where语句.
WITH stuff1 AS(从名为startdate> 0的员工中选择名称,startdate,id)
WITH stuff2 AS(从stuff1中选择名称,startdate,id)
select*from stuff2,其中id> 10
Cade Roux.. 30
我一直这样做:
WITH stuff1 AS ( SELECT name ,startdate ,id FROM employees WHERE startdate > 0 ) ,stuff2 AS ( SELECT name ,startdate ,id FROM stuff1 ) SELECT * FROM stuff2 WHERE id > 10
据我所知,我没有达到CTE的限制.
你唯一不能做的事情(这将是非常有用的)是在单独的SELECT
s中重用CTE :
WITH stuff1 AS ( SELECT name ,startdate ,id FROM employees WHERE startdate > 0 ) ,stuff2 AS ( SELECT name ,startdate ,id FROM stuff1 ) SELECT * FROM stuff2 WHERE id > 10 ; SELECT * FROM stuff2 WHERE id < 10
说.相反,您必须再次复制并粘贴整个CTE链.
我一直这样做:
WITH stuff1 AS ( SELECT name ,startdate ,id FROM employees WHERE startdate > 0 ) ,stuff2 AS ( SELECT name ,startdate ,id FROM stuff1 ) SELECT * FROM stuff2 WHERE id > 10
据我所知,我没有达到CTE的限制.
你唯一不能做的事情(这将是非常有用的)是在单独的SELECT
s中重用CTE :
WITH stuff1 AS ( SELECT name ,startdate ,id FROM employees WHERE startdate > 0 ) ,stuff2 AS ( SELECT name ,startdate ,id FROM stuff1 ) SELECT * FROM stuff2 WHERE id > 10 ; SELECT * FROM stuff2 WHERE id < 10
说.相反,您必须再次复制并粘贴整个CTE链.