Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a trailing comma after the last field matter when creating a table using T-SQL?

Shouldn't one of these statements work and one fail?

Intuition says Statement 2 should fail because there is a comma after int and no second column listed.

Yet both work and the trailing comma "," after the last column data type makes no difference.

-- Statement 1
CREATE TABLE dbo.MyTable1( col1 int);

-- Statement 2
CREATE TABLE dbo.MyTable2( col1 int,);

However (and this is expected): two commas ",," after the last field do cause a failure:

-- Statement 3
CREATE TABLE dbo.MyTable3( col1 int,,);

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

Testing shows that its not just any character after the last field that is allowed through. For example, this fails:

-- Statement 3
CREATE TABLE dbo.MyTable3( col1 int ~);

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

Maybe SQL Server is "saving a seat at the table" for something? The Primary Key perhaps? I really don't know.

I am using Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64).

like image 385
Jon Jaussi Avatar asked Jan 06 '14 23:01

Jon Jaussi


People also ask

What happens if a comma is placed at the end of a line in an SQL program?

Commas in T-SQL are used to format numbers, as list-separators, and value separators. When commas are used as separators, there is no specific rule for placing commas at the end of a line or at the beginning.

How do you put a comma after every line in SQL?

In SSMS you can hold SHIFT+ALT and then select multiple lines with a mouse or go up and down with the arrow/cursor keys to edit across multiple lines, and add the comma.

What is trailing comma?

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma.

Can trailing commas be used in objects and arrays?

A trailing comma, also known as a dangling or terminal comma, is a comma symbol that is typed after the last item of a list of elements. Since the introduction of the JavaScript language, trailing commas have been legal in array literals. Later, object literals joined arrays.


1 Answers

See http://connect.microsoft.com/SQLServer/feedback/details/273348/trailing-comma-allowed-in-create-table:

Description

When executing the CREATE TABLE command, a trailing comma following the last column is allowed. Based on the grammar in BOL and comma usage in lists in other T-SQL statements, this behavior is inconsistent. This is a very minor issue and does not appear to cause any adverse side-effects. It just appears that the parser may be a bit off.

Microsoft views this as a bug, but a minor one.

This was resolved some time ago as "won't fix" but we didn't explain why. Simply, this seems pretty harmless, and not worth fixing in a service pack. We may consider fixing this in a future release.

like image 144
Robert Harvey Avatar answered Jan 03 '23 00:01

Robert Harvey