Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Creating Index on @table

Author  Topic 

cjpsx
Starting Member

34 Posts

Posted - 2006-06-08 : 14:20:19
I have declared a table in TSQL:

DECLARE @tempTable1 TABLE

I'm trying to create an index on this "table" but do not know if it is even possible:

CREATE INDEX idx1 ON @tempTable1 (invoiceid)

Is this possible?


Thanks,
cj

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-06-08 : 14:25:39
You can not add an index to the table variable, however you can add a primary key constraint to the table variable, that is if invoiceid is unique. You get an index with a PK constraint.

DECLARE @tempTable1 TABLE (invoiceid int PRIMARY KEY CLUSTERED)


Tara Kizer
aka tduggan
Go to Top of Page

cjpsx
Starting Member

34 Posts

Posted - 2006-06-08 : 14:37:42
Thank you. I will try that.

Are there any performance issues between a table variable and a #temp table?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-06-08 : 14:42:14
Yes. But you'll need to google that as it has been discussed at length already. SQL Server Books Online indicates that you should use table variables usually. Other people have argued against that. After doing your research, I'd recommend testing both scenarios to determine which will work better for you in your particular situation. Each situation will different. You might have some that perform better with table variables and some with temporary tables.

Tara Kizer
aka tduggan
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-09 : 10:19:21
quote:
Originally posted by cjpsx

Thank you. I will try that.

Are there any performance issues between a table variable and a #temp table?


Read this
http://www.aspfaq.com/show.asp?id=2475

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-06-09 : 11:09:11
The need to use an index on a temp table or a table variable is probably a flawed concept...usually they are driver tables that will usually be scanned, so no need for an index. Maybe you should describe what you are trying to do



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page
   

- Advertisement -