x

Can I create an index or constraint on a table variable?

Site seeder question: I am using a table variable in one of my queries:

DECLARE @intermediateData TABLE (
  ID        INT IDENTITY (1,1),
  KeyData   VARCHAR (255),
  ValueData VARCHAR (255));

Is it possible for me to create an index on the ID column?

more ▼

asked Oct 13, 2009 at 09:35 AM in Default

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

You can create a primary key when you define the table variable, but you can't create an index on that column.

more ▼

answered Oct 13, 2009 at 10:03 AM

avatar image

Grant Fritchey ♦♦
137k 20 42 81

(comments are locked)
10|1200 characters needed characters left

I'm just adding this because I was hoping for some syntax examples. A clustered primary key would be as follows:

DECLARE @intermediateData TABLE (
  ID        INT IDENTITY (1,1) PRIMARY KEY CLUSTERED,
  KeyData   VARCHAR (255),
  ValueData VARCHAR (255));

A unique constraint can also be defined through the following:

DECLARE @intermediateData TABLE (
  ID        INT IDENTITY (1,1),
  KeyData   VARCHAR (255),
  ValueData VARCHAR (255)
  UNIQUE (KeyData, ValueData));
more ▼

answered Oct 13, 2009 at 01:50 PM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x144
x26

asked: Oct 13, 2009 at 09:35 AM

Seen: 6612 times

Last Updated: Oct 13, 2009 at 09:35 AM

Copyright 2016 Redgate Software. Privacy Policy