snippetsqlMinor
Make "as" mandatory when create column alias?
Viewed 0 times
mandatorycreatecolumnmakewhenalias
Problem
We found a lot of bugs are caused by missing comma,
Is it a way to make
select
aaaa,
bbbb -- forgot comma
ccc,
dddddddd,
.....Is it a way to make
expression name invalid and it must be expression as name in a SQL statement? (not globally because of missing "as" exists legacy code)Solution
No, there is no way to force that syntax, unless you pass all queries through some 3rd party tool that parses the SQL code and supports looking for syntax like this, but I think it will be very difficult to even approach writing something that will do this.
It is also not possible to stop someone from typing
In addition, I find the
At the end of the day, though, it's a preference, and no, there is no way to force it. Don't let your developers blame the language for their sloppiness. This is something that should be caught before a second set of eyes ever sees that query. I mean, how hard is it to count how many columns are returned by your query?
It is also not possible to stop someone from typing
FROM dbo.table nolock and not realizing that in that case nolock is an alias, not a locking hint. That is why this works:SELECT name FROM sys.objects nolock WITH (NOLOCK);In addition, I find the
expression AS alias syntax far less usable than alias = expression - since, when I'm reviewing or troubleshooting code, I'm much more likely to need to find a column by its alias at the beginning of a line, than by reading what could be a very long and complex expression, that spans lines or goes off the screen to the right, and ends at a different vertical point than all the other lines. And since aliases should be shorter than expressions, it makes more sense to me to put the alias first. At the end of the day, though, it's a preference, and no, there is no way to force it. Don't let your developers blame the language for their sloppiness. This is something that should be caught before a second set of eyes ever sees that query. I mean, how hard is it to count how many columns are returned by your query?
Code Snippets
SELECT name FROM sys.objects nolock WITH (NOLOCK);Context
StackExchange Database Administrators Q#52846, answer score: 9
Revisions (0)
No revisions yet.