What is the proper syntax for a sql table variable with more than one index on it?

I want to create a simple Table Variable with 2 fields in it and 2 indexes on it.

The first field requires a UNIQUE CLUSTERED INDEX.

The second field requires a NONCLUSTERED INDEX.

DECLARE @Users TABLE (
[UserId] [bigint] PRIMARY KEY,      
[UserTypeId] [int] NOT NULL,
UNIQUE CLUSTERED (UserId))

CREATE NONCLUSTERED INDEX IDX_Users_UserTypeId ON @Users(UserTypeId)

I'm getting this error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '@Users'.

What is the proper syntax for a sql table variable with more than one index on it?
MariaHaltAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
You cannot explicitly create  indexes on table variable, you need to create temp table for this.
If you just need to create a clustered index,  declare it as primary key; but you cant create any other index on a table variable
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Are you really searching the table by all three columns?

If not, create a clustered index on the main way you search, and, if necessary, non-clus indexes on the other ways.  

You'll need to use a temp table rather than a table variable.  Indeed, if you plan to search a work table multiple times, you should always use a temp table rather than a variable table.
0
Randy Knight, MCMPrincipal ConsultantCommented:
The other thing I'd add is that since you are indexing it, it sounds like this table variable will have a lot of rows?  If so, a temp table is always more efficient since SQL Server ends up spilling the table variable to work tables anyway.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
Huh?  SQL handles the physical storage of either a temp table or a table variable identically.

The main difference for performance is that stats are created on a temp table, but not on table variables.  While temp stats can cause some recompiles, it will also often generate a much better query plan than not having stats at all.
0
Randy Knight, MCMPrincipal ConsultantCommented:
This is an old article but still applicable.

http://support.microsoft.com/kb/305977/en-us
0
Scott PletcherSenior DBACommented:
Regarding which is preferred, my experience has been that temp tables almost always perform better.  I use table variables only for very small datasets (10 rows or fewer) and in cases where I need rows to never be rolled back.  Otherwise, my suggestion is to stick to temp tables.
0
Randy Knight, MCMPrincipal ConsultantCommented:
Agree with Scott on all counts.  Table variables = very small data set that you're only going to use once.
0
MariaHaltAuthor Commented:
Thanks everyone for answering.  I had been using a temp table with the two indexes on it but the stored procedure it resides in was taking over a minute to complete.  Through trial and error, uncovered the temp table as the bottleneck.  So, I decided to try the table variable and it improved it vastly.  It only takes 1 second with one index, the unique one.  I got greedy, thought I could do better.  As for the table size, it could go a couple of thousand records.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.