是否可以访问在公用表表达式的查询中定义的临时列?说我有
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。
答案 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
您的示例看起来很好,当您尝试使用该功能时是否出现错误?