Compound primary key in Table type variable
Asked Answered
C

1

47

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?

Coffelt answered 14/9, 2009 at 11:0 Comment(0)
O
101

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)
);
Oeflein answered 14/9, 2009 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.