简体   繁体   中英

How to join to an alias column in SQL Server?

I have the followig problem. When the input is empty I want to replace it with the 'yellow_code' column aliased as 'Code'. When I attempt an inner join on the aliased column 'Code' to 'GrantCode' on another table, I get the following error description: "Invalid column name 'Code'".

Anyways to bypass this?

The SELECT-column alias is practically the last thing to be applied to the statement and as such is not available for joins "lower down" that form part of the query as a whole. You can however access the alias if referenced from an outer select

eg

select my_code from
(
select 1 as my_code from ....
) x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM