December 8, 2005 at 1:23 pm
I want to create a procedure to query my tables to identify all of the Indexes.
The information that I would like to see would be the Index Name, it's fields & direction. I also want to know which index is the clustered one (can I have more than one?).
I would like to get the Primary Key info, too.
This is more for me to create documentation from.
Would there be any other info that would be helpful? I might like to see the relationships between tables (if there are any).
Thank you,
Bryan Clauss
December 8, 2005 at 2:08 pm
Does this work for u
sp_helpindex <tablename>
it gives the indexname , index description, index keys
December 8, 2005 at 2:11 pm
A starting point would be to use sp_helpindex.
This tells you some information about the indexes for a given table.
If you wish to expand on what this gives you, you can script that stored procedure into query analyser (just find it in the master db and right click then follow your nose).
Once you have it scripted, it is not too difficult to change the queries (don't try to recreate the stored proc though) to suit. It also gives you an insight into how and where SQL Server puts all of its object references
December 8, 2005 at 6:35 pm
Would it be a great idea to read some headlines on main page before entering forum?
http://www.sqlservercentral.com/columnists/AVigneau/boostyourperformancebadindexdetection.asp
_____________
Code for TallyGenerator
December 9, 2005 at 7:37 am
Also look at the information_schema views - look in BOL index for "information schema views". These are the nice and supported way to retrieve metadata from SQL - although I'm not sure if they give you the indices.
December 9, 2005 at 12:36 pm
Bryan,
rsharma posted a script for info on indexes, I think it would help you. Under sql server admin: 5th thread.
Jules
Jules Bui
IT Operations DBA
Backup and Restore Administrator
December 10, 2005 at 11:39 am
Here's a script I wrote to standardize the names of the PKs - you can probably modify it to pull any other info you want from the system table
select 'EXECUTE sp_rename ''' + b.name + ''', ' + '''PK_' + a.name + ''', ' + '''OBJECT''' from sysobjects a inner join sysobjects b on a.id = b.parent_obj where b.type = 'K' and b.name not in ('pk_dba_replication', 'pk_dtproperties')
Similarly, here's one for the indexes:
SELECT CASE WHEN c.keyno = 1 THEN 'EXECUTE' ELSE 'COMPOUND INDEX' END + ' sp_rename ''' + a.name +'.' + b.name + ''', ' + '''IX_' + a.name + '_' + d.name + ''', ' + '''INDEX'''
--select b.name, a.name, d.name, C.*
FROM sysobjects A join sysindexes B on a.id = b.id
join sysindexkeys C on B.id = C.id and b.indid = c.indid
join syscolumns d on c.id = d.id and c.colid = d.colid
where a.id > 100
and b.keycnt > 1
and c.keyno = 1 -- remove this line to see an addl row for compound indexes and b.name not like '_WA%'
and b.name not like 'PK%'
and a.name <> 'MSreplication_subscriptions'
and a.name <> 'MSsubscription_agents'
order by a.name, b.name
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply