Table类型变量中的复合主键

时间:2009-09-14 11:00:40

标签: sql-server-2008 variables primary-key

SQL Server 2008:

DECLARE @MyTable TABLE(
    PersonID INT NOT NULL,
    Person2ID INT NOT NULL,
    Description NVARCHAR(100),
CONSTRAINT PK PRIMARY KEY CLUSTERED (PersonID, Person2ID)
);

给出:

Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'CONSTRAINT'.

有没有办法在Table值变量中使用复合主键?

1 个答案:

答案 0 :(得分:91)

您可以像这样定义复合主键:

DECLARE @MyTable TABLE
(   
    PersonID INT NOT NULL,    
    Person2ID INT NOT NULL,    
    Description NVARCHAR(100),
    PRIMARY KEY (PersonID, Person2ID)
);