Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find a list of indexes on a temporary table that I've created in SQL Server 2012

Please can some one help me to get a list of indexes on a temporary table that I've created in SQL Server 2012

like image 604
PeterO Avatar asked Jan 31 '26 10:01

PeterO


2 Answers

CREATE TABLE #tmpTable (ID BIGINT PRIMARY KEY, INDEXCOLUMN BIGINT)

IF NOT EXISTS(SELECT * FROM tempdb.sys.indexes WHERE name = 'IX_TMPINDEX' AND OBJECT_ID = object_id('tempdb..#tmpTable'))
BEGIN
CREATE NONCLUSTERED INDEX IX_TMPINDEX ON #tmpTable (INDEXCOLUMN)    
END

GO 

SELECT * FROM tempdb.sys.indexes WHERE OBJECT_ID = object_id('tempdb..#tmpTable')
like image 134
Rakdos Avatar answered Feb 03 '26 01:02

Rakdos


Here's one way, using sp_helpindex:

CREATE TABLE #temp (id int, val1 int)

CREATE INDEX ix_t1 on #temp (id)

EXEC tempdb.dbo.sp_helpindex '#temp'
like image 38
Ed Harper Avatar answered Feb 03 '26 00:02

Ed Harper