原因别名适用于 ORDER BY 但不适用于 WHERE
Reason alias works in ORDER BY but not in WHERE
我理解 I can't reference an alias in the WHERE clause,但这是为什么呢?是否有不同的解释?
这样的事情会产生错误:
declare @myTable table
(
num numeric(5,2),
den numeric(5,2)
)
insert into @mytable
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1
select num/den as 'calc' from @myTable
where calc is not null
order by calc
但是这 returns 行:
declare @myTable table
(
num numeric(5,2),
den numeric(5,2)
)
insert into @mytable
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1
select num/den as 'calc' from @myTable
--where calc is not null
order by calc
如中所述,这是由于自然查询处理顺序:
FROM
ON
OUTER
WHERE
GROUP BY
CUBE
| ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
我理解 I can't reference an alias in the WHERE clause,但这是为什么呢?是否有不同的解释?
这样的事情会产生错误:
declare @myTable table
(
num numeric(5,2),
den numeric(5,2)
)
insert into @mytable
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1
select num/den as 'calc' from @myTable
where calc is not null
order by calc
但是这 returns 行:
declare @myTable table
(
num numeric(5,2),
den numeric(5,2)
)
insert into @mytable
select 1, 2
union
select 1, 3
union
select 2, 3
union
select 2, 4
union
select 2, 5
union
select null, 1
select num/den as 'calc' from @myTable
--where calc is not null
order by calc
如
FROM
ON
OUTER
WHERE
GROUP BY
CUBE
|ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP