SQL Server:创建表变量,主键约束定义中的错误

问题描述:

我正在尝试使用主键创建一个表变量.这两个语句有什么区别?为什么第一个不起作用?

I'm trying to create a table variable with primary key. What is the difference between these two statements? Why the first one does not work?

--This does not work
declare @tabvar table (
    rowid int identity(1, 1) not null,
    var1 int null,
        constraint PK_tabvar_rowid primary key clustered (rowid)
)

--This works
declare @tabvar1 table (
    rowid int identity(1, 1) not null primary key clustered,
    var1 int null
)

类似的行为在 BOL 中有很好的记录.查看 DECLARE TABLE变量

Looks like this behavior is well documented in BOL. Look at the syntax definitions for DECLARE TABLE Variable vs CREATE TABLE.