3

I am having an issue where I am working on table aliases and ran into a weird issue. I am able to use null as column name when I am not using table alias, but if I use a table alias I run into issues.

works!

select top 10 eid, null as emp_no from employee 

does not work!!

select top 10 e.eid, null as e.emp_no from employee e

Is there a way out? I am running into an issue when I join with another table.

Trying to make it work !

select top 10 e.eid, null as e.emp_no, ed.desgination from employee e 
inner join employee_designations ed on e.eid=ed.eid


Error 

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '.'.
4

2 回答 2

9

尝试:

select top 10 e.eid, null as emp_no from employee e

您可以 alias NULL,但如果您声称它来自表 alias ,SQL 将抛出一个 hissy-fit e

更新

NULL在连接中使用,请将其转换为子查询:

SELECT  e.eid
        , e.emp_no
FROM    (
            SELECT  TOP 10 eid
                    , NULL AS emp_no
            FROM    employee
        ) AS e

将您的子查询命名为 as e,现在 SQL 将很乐意接受NULLas e.emp_no

于 2013-11-08T01:41:24.677 回答
0

我不是 100% 确定,但是会抛出这个错误,因为当你为一个列分配一个值时,这不再是你的表列,只是一个“虚拟”列,所以这就是为什么你不能给表别名到这个列,仅仅因为这不是一个列表

为什么?如果表具有相同的列名,它会返回Ambiguous column name,但是您正在分配一个值,因此您没有获得此列名,您可以null as emp_no在查询合成器中简单地做,因为这个 emp_no 不是内部连接合成器中的任何列,尝试调用:

select top 10 e.eid, null as emp_no, ed.desgination from employee e 
inner join employee_designations ed on e.eid=ed.eid
于 2013-11-08T01:57:40.017 回答