Code analysis incorrect - alias
ggeier
Posts: 30 Bronze 2
in SQL Prompt
The code analysis for aliases isn't correct. For example, this query:
Gives this issue: Old-style column alias via EQUAL sign. It is recommended to specify alias via AS. SQL uses the AS keyword to specify an alias. The assignment syntax is deprecated.
It's not a deprecated feature though according to: https://docs.microsoft.com/en-us/sql/database-engine/deprecated-database-engine-features-in-sql-server-2017
'string_alias' = expression is deprecated, but not column_alias = expression.
select my_alias = o.name from sys.objects o
Gives this issue: Old-style column alias via EQUAL sign. It is recommended to specify alias via AS. SQL uses the AS keyword to specify an alias. The assignment syntax is deprecated.
It's not a deprecated feature though according to: https://docs.microsoft.com/en-us/sql/database-engine/deprecated-database-engine-features-in-sql-server-2017
'string_alias' = expression is deprecated, but not column_alias = expression.
Tagged:
Best Answer
-
Tianjiao_Li Posts: 684 Rose Gold 5Hi @ggeier ,
Thanks for reporting it! We've logged it as a bug and please keep an eye on our release note!
Answers
Under Style rules, I think ST002 (Old-style column alias via EQUAL sign) is wrong for the same reason. The help claims that this style of alias is deprecated - it isn't, and afaik there is no reason to flag this style up as problematic.
The only argument I'm aware of for not using it is that it's SQL Server specific. If the rule is going to remain, could the help files perhaps be updated to reflect this?
I know it's a less popular style, but I greatly prefer it for readability reasons - and I'm in good company.
http://sqlblog.com/blogs/aaron_bertrand/archive/2012/01/23/bad-habits-to-kick-using-as-instead-of-for-column-aliases.aspx
One detail in the description ought to be clarified. It is only the 'string_alias' = <expression> syntax that is deprecated. For now the <column_alias> = <expression> is still there but is liable to be deprecated in future releases because it is non-standard. The SQL standards and Microsoft both say we should use the AS syntax.
It would be a real shame if it happened as it's so much more readable having the aliases first in an aligned list rather than having to find them at the end of the columns. I'm a late convert to this style, having moved over from Oracle, and then used the standard syntax the first couple of years I was on SQL Server. Despite years of using the AS syntax I find this much easier to read and a big time saver.
I've never given the portability issue much weight - in the highly unlikely event I had to move all my SQL to a different platform, changing alias styles would be the least of my worries! And I could switch them all using SQLPrompt before migrating.
Obviously if it were deprecated I'd have to switch at some point regardless of it being beneficial, so it would be good to know if I've missed something.
I wouldn't count it as a code smell. It's not ANSI, but the expression as alias isn't remotely readable in complex code. Once you have expressions that grow and don't format well in the horizontal space, finding the alias can be cumbersome.
Most of these checks are merely advisory, and SQL Prompt evidently allows you to switch on and off all the Code checks individually. It is always down to individual judgement and preferences. After all, some people still like using the old Sybase inner join syntax which is still likewise supported by SQL Server and free from deprecation (unlike the sybase outer join syntax!) . I agree that the description should be elaborated to point out that <column_alias> = <expression> is still supported.
Was either 'string_alias' = <expression> or <column_alias> = <expression> ever in the SQL Standard? I've looked back to 1999 but can't find it.
There's a menu option
This issue is fixed now. Please upgrade to the latest version!
Thanks.
Tianjiao Li | Redgate Software
Have you visited our Help Center?