CTE - 我可以使用多个临时结果集吗?

时间:2014-07-11 12:22:18

标签: sql-server common-table-expression

我想使用CTE创建临时结果集。然后,我想创建另一个使用第一个结果集作为输入的临时结果集。这可能吗?下面的代码试图这样做,但我遇到错误:“无效的列名'ptask_id'” 提前谢谢。

;WITH act_and_proc
 AS (SELECT *
       FROM afm_roleprocs
      WHERE role_name = 'GDI SYSTEM ADMIN'),

ptask_list AS
(SELECT ptask_id,
   activity_id,
   process_id,
   task_file
 FROM afm_ptasks pt
 WHERE     process_id IN (SELECT process_id
                        FROM act_and_proc ap
                       WHERE     pt.process_id = ap.process_id
                             AND pt.activity_id = ap.activity_id
                             AND pt.task_type = 'WEB URL'
                             AND pt.security_group != 'SYS-HIDDEN'
                             AND pt.is_hotlist = 0)
   AND process_id IN (SELECT process_id
                        FROM afm_processes
                       WHERE process_id = pt.process_id AND is_active = 1)
)

select * from ptask_list

1 个答案:

答案 0 :(得分:1)

不确定。语法如下所示。

with MyCte as
(
    Select SomeColumns from SomeTable
)
, MyNextCte as
(
    Select OneColumn from MyCte
)

select * from MyNextCte