Results 1 to 5 of 5

Thread: Composite Key on Table Data Type

  1. #1
    Join Date
    Sep 2002
    Posts
    3

    Question Composite Key on Table Data Type

    I am trying to add indexes to my table data types and have realized that I can only add primary keys. So, I am hoping there is a way to add a composite primary key, but I am not having any success. I have tried the following:

    Declare @Sales TABLE
    (SaleID INT IDENTITY(100000,1),
    SalesRegion CHAR(2),
    CONSTRAINT ID_PK PRIMARY KEY (SaleID,SalesRegion))

    Any suggestions would be appreciated.

    Thanks!

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Indexes or other constraints applied to the table data type must be defined as part of the DECLARE variable or CREATE FUNCTION statement. They cannot be applied later, because the CREATE INDEX or ALTER TABLE statements cannot reference table variables and user-defined functions. Check 'DECLARE @local_variable' in books online.

  3. #3
    Join Date
    Sep 2002
    Posts
    3
    Rmiao,

    I realize this is true. That is why in the above example I added the constraint as part of the declare statement. Are you saying that you think it can not be done? Or do you have an example of how to do it. I have reviewed the SQL documentation for Create Table and Declare many times before posting this.

    Thanks!

  4. #4
    Join Date
    Sep 2002
    Location
    DALLAS
    Posts
    25
    Declare @Sales TABLE
    (SaleID INT IDENTITY(100000,1),
    SalesRegion CHAR(2),
    PRIMARY KEY (SaleID,SalesRegion))

  5. #5
    Join Date
    Sep 2002
    Posts
    3
    WizKid,

    This is exactly what I wanted. I just coudn't get the syntax right. Thanks so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •