views:

88

answers:

2

Is the following possible? I am unable to do so. Do I have to have a permanent table to create index?

declare @Beatles table
    (
     LastName varchar(20) ,      
     FirstName varchar(20) 
    )

CREATE CLUSTERED INDEX Index_Name_Clstd ON @Beatles(LastName)
+1  A: 

Not on a table variable, but on a temp table see this http://www.sqlteam.com/article/optimizing-performance-indexes-on-temp-tables

JonH
that works for **temp tables** - but not for table variables!
marc_s
marc i misread it and was changing it when you replied..god this site is awfully fast!
JonH
@Jonh: <hehe> you gotta think and type at approx. the speed of light here!
marc_s
+2  A: 

No, you cannot create indices on a table variable - see this article here and this posting here comparing local, global temporary tables to table variables.

Restrictions

You cannot create a non-clustered index on a table variable, unless the index is a side effect of a PRIMARY KEY or UNIQUE constraint on the table (SQL Server enforces any UNIQUE or PRIMARY KEY constraints using an index).

marc_s