使用T-SQL创建表时,为什么最后一个字段后面的逗号不重要? [英] 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?
直觉上说陈述2 应该失败,因为 int ,并且没有列出第二列。
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 '~'.
也许SQL Server为某些事情节省了席位?主键也许?我真的不知道。
Maybe SQL Server is "saving a seat at the table" for something? The Primary Key perhaps? I really don't know.
我正在使用Microsoft SQL Server 2008 R2(SP1)-10.50.2500.0(X64)。
I am using Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64).
推荐答案
描述
在执行CREATE TABLE命令时,$ b之后的逗号$ b允许最后一列。基于BOL中的语法以及其他T-SQL语句中列表中逗号
的用法,此行为是
不一致的。这是一个非常小的问题,似乎不会给
带来任何不利的副作用。似乎解析器可能会折价
。
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将此视为错误,但此错误较小。
Microsoft views this as a bug, but a minor one.
前段时间已将其解决为无法解决,但我们没有解释原因。简而言之,这似乎很无害,不值得在Service Pack中进行修复。我们可能会考虑在将来的版本中修复此问题。
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.
这篇关于使用T-SQL创建表时,为什么最后一个字段后面的逗号不重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!