T-SQL missing comma in SELECT does not generate parsing error

I ran in to this “gotcha” the other day while writing a number of views. I omitted a comma on my SELECT statement, and the parser did not return an error, but rather just results that I did not expect. The problem is that SQL Server interprets the missing comma as an implied column name. It is as if there is an unwritten “AS” where the missing comma should go.

Screenshots of the three scenarios:

Desired query with the correct syntax

The correct query returns the desired results: two columns of information

Accidentially leaving off the comma results in only one column (with the wrong data)

Accidentally leaving off the comma results in only one column (with the wrong data)

Adding in an explicit "AS" gives the same results as omitting the comma

Adding in an explicit "AS" gives the same results as omitting the comma

I’ve been working with SQL for years and never had this issue before. I supposed it is because if I make a mistake like this, I usually omit more than one comma, which causes a parsing error.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>