Thursday 8 November 2012

This script will list out the details of the indexes in a table.This is compatible to SQL 2000 and 2005.
Script

create table #indexdetails (tablename varchar(200),indexname varchar(300), indexdesc varchar(300), indexkeys varchar(300))
declare @tablename varchar(100)
DECLARE index_cursor CURSOR FOR
SELECT name from sysobjects where xtype = 'U'
open index_cursor
FETCH NEXT FROM index_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
insert into #indexdetails (indexname,indexdesc,indexkeys) exec sp_helpindex @tablename
update #indexdetails set tablename = @tablename where tablename IS NULL
FETCH NEXT FROM index_cursor
INTO @tablename
end
close index_cursor
deallocate index_cursor
select * from #indexdetails order by tablename
drop table #indexdetails

No comments:

Post a Comment