title: Composite Primary Key On Table Variable In Stored Procedure
body: <p>I'm new to stored procedures and trying to add a composite primary key to a table variable.</p>
  
  <pre><code>DECLARE @statistictemp TABLE (
      MajorName      VARCHAR(50) NOT NULL, 
      SubName       VARCHAR(50) NOT NULL, 
      DetailedName   VARCHAR(50) NOT NULL, 
      UniversityID   SMALLINT    NOT NULL, 
      StatisticValue DECIMAL(9,3)
  );
  
  ALTER TABLE @statistictemp 
  ADD CONSTRAINT pk_statistictemp 
  PRIMARY KEY (MajorName, SubName, DetailedName, UniversityID);
  </code></pre>
  
  <p>I'm getting an error on <code>ALTER TABLE @statistictemp</code> saying </p>
  
  <blockquote>
    <p><code>Incorrect syntax near '@statistictemp'. Expecting ID, QUOTED_ID, or
    '.'.</code></p>
  </blockquote>
  
  <p>What am I doing wrong here? How do you add a composite primary key to a table variable?</p>