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