简体   繁体   English

如何加入SQL Server中的别名列?

[英]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'. 当输入为空时,我想用'yellow_code'列替换它,将其替换为'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'". 当我在另一个表上的别名列'Code'上尝试内部连接到'GrantCode'时,我得到以下错误描述:“无效的列名'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. SELECT-column别名实际上是应用于语句的最后一件事,因此不适用于作为整体构成查询一部分的“lower down”连接。 You can however access the alias if referenced from an outer select 但是,如果从外部选择引用,则可以访问别名

eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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