T-SQL:访问公用表表达式中的临时列

时间:2012-02-10 22:06:19

标签: sql-server tsql common-table-expression

是否可以访问在公用表表达式的查询中定义的临时列?说我有

select * from myTable 

;with cte  as 
(
    select
        *, Salary * 4 as FourTimesSalary
    from 
        Employees
    where 
        Name = @name
        and ID >= 100 
)

有没有办法在查询cte时使用临时列FourTimesSalary

select top 2 *  
from cte  
order by FourTimesSalary, Name

TIA。

1 个答案:

答案 0 :(得分:1)

是的,你可以这样做。例如:

with temp as
(
    select 1 as id, 2*4 as val
    UNION
    select 2 as id, 3*4 as val
)
SELECT * FROM temp ORDER BY VAL desc

您的示例看起来很好,当您尝试使用该功能时是否出现错误?