Compound primary key in Table type variable

血红的双手。 提交于 2019-11-29 02:06:41

问题


SQL Server 2008:

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

Gives:

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

Is there any way to have compound Primary key in Table valued variables?


回答1:


You can define a composite primary key like this:

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


来源:https://stackoverflow.com/questions/1420897/compound-primary-key-in-table-type-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!