Groups | Blog | Home
all groups > sql server odbc > february 2004 >

sql server odbc : SQL statement to list table indexes?


Sloan Thrasher
2/5/2004 1:04:45 AM
Hi all!

Is there a way to get a list of indexes via a SQL statement? I'm working on
a DB documentor, and am able to retrieve almost everything else about the
definitions, but I haven't been able to get a list of indexes.

Thanks!

Sloan

Jacco Schalkwijk
2/5/2004 3:10:10 PM
SELECT OBJECT_NAME(id) AS table_name, name as index_name
FROM sysindexes
WHERE indid BETWEEN 1 AND 254
AND OBJECTPROPERTY(id, 'IsMSShipped') = 0
AND INDEXPROPERTY(id, name, 'IsStatistics') = 0

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

Joe Wickers
2/5/2004 4:38:55 PM

When your done why don'e you post the code. Sounds like it would be handy.

[quoted text, click to view]
Sloan Thrasher
2/6/2004 9:20:30 AM
Hi Jacco!

Thanks! I should have been a bit more specific -- sorry.

I'm actually looking for the details i.e. which columns are involved,
asending/desending, etc.

Thanks!

Sloan

[quoted text, click to view]

Sloan Thrasher
2/6/2004 5:34:30 PM
Hi Joe!

At the moment, the code is pretty application specifc. I'm having to exclude
some tables and stored procs by name since they're part of the debugging
process and not part of the actual delivered system. I've hard coded their
names at the moment, but will probably change that so that they are in a
table instead. What I've got is a single SP that outputs finished HTML code.
It's called by a really simple ASP page that just takes the output and sends
it to the browser. Once I get past this tight deadline, I'll try and rework
it to be of more general use, and post the code.

Sloan

[quoted text, click to view]

Jacco Schalkwijk
2/9/2004 10:04:40 AM
Hi Sloan,

This should give you (most of) the information you are looking for:

SELECT
OBJECT_NAME(sik.id) AS Table_Name,
si.name AS Index_Name,
col.name AS column_name,
sik.keyno,
CASE INDEXKEY_PROPERTY(sik.id , sik.indid, sik.keyno , 'IsDescending')
WHEN 1 THEN 'DESC'
WHEN 0 THEN 'ASC'
END AS sort_order
FROM sysindexkeys sik
INNER JOIN sysindexes si
ON sik.id = si.id
AND sik.indid = si.indid
INNER JOIN syscolumns col
ON sik.id = col.id
AND sik.colid = col.colid
WHERE sik.indid BETWEEN 1 AND 254
AND OBJECTPROPERTY(sik.id, 'IsMSShipped') = 0
AND INDEXPROPERTY(sik.id, si.name, 'IsStatistics') = 0

ORDER BY table_name, index_name, keyno

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

Sloan Thrasher
2/9/2004 3:54:49 PM
Thanks Jacco!!

[quoted text, click to view]

AddThis Social Bookmark Button