开发者

Different ways to alias a column

What is the difference between

select  empName as EmployeeName from employees

versus

select  EmployeeName = empName from employees

from a technical point of vie开发者_C百科w. Not sure if this is just SQL server specific or not.

Appreciate your answers.


I'd prefer the first one, since the second one is not portable -

select  EmployeeName = empName from employees

is either a syntax error (at least in SQLite and Oracle), or it might not give you what you expect (comparing two columns EmployeeName and empName and returning the comparison result as a boolean/integer), whereas

select  empName EmployeeName from employees

is the same as

  select  empName as EmployeeName from employees

which is my preferred variant.


The main advantage of the second syntax is that it allows the column aliases to be all lined up which can be of benefit for long expressions.

SELECT foo,
       bar,
       baz = ROW_NUMBER() OVER (PARTITION BY foo ORDER BY bar)
FROM T


I don't think there's a technical difference. Its mainly preferential. I go for the second as its easier to spot columns in big queries, especially if the query is properly indented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜