473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

list of clustered index in the database

hi guru's

would appreciate if someone could show how to list all the clustered
indexes in the database.

if it can done as a output of single query it would be fine. the output
should be the table name, column name and clustered index name.

thanx
bala

Aug 31 '05 #1
6 8666
Stu
I don't think it can be done in a single query, but you can do it like
so:

--declare variables and temp table for accumulation
DECLARE @tName varchar(200)
CREATE TABLE #t (table_name varchar(200),
index_name varchar(200),
index_description varchar(210),
index_keys nvarchar(2078))

--open cursor for user tables
DECLARE C CURSOR LOCAL FOR
SELECT name
FROM sysobjects
WHERE xtype = 'U'

OPEN C

FETCH NEXT FROM c INTO @tname

WHILE @@FETCH_STATUS = 0
BEGIN

--run sp_helpindex against table in cursor
INSERT INTO #t (index_name, index_description, index_keys)
exec sp_helpindex @tname

--since sp_helpindex doesn't return a table name,
--have to update the current NULL table_name
UPDATE #t
SET table_name = @tname
WHERE table_name is NULL

--Loop by getting next row from cursor
FETCH NEXT FROM c INTO @tname
END

CLOSE C

DEALLOCATE C

--retrieve specified data; limit it to clustered indexes
SELECT table_name, index_name, index_keys
FROM #t
WHERE index_description like 'clustered%'

DROP TABLE #t

HTH,
Stu

Sep 1 '05 #2
hey stu

thanx for the quick response. will try it out tomorrow in office

regards
bala

Sep 1 '05 #3
This should work too.

select object_name(id) as table_name, name as index_name
from sysindexes
where indid = 1

Sep 1 '05 #4
bala (ba*****@gmail.com) writes:
would appreciate if someone could show how to list all the clustered
indexes in the database.

if it can done as a output of single query it would be fine. the output
should be the table name, column name and clustered index name.


Here is a query:

SELECT tblname = CASE WHEN ik.keyno = 1 THEN o.name ELSE '' END,
ixname = CASE WHEN ik.keyno = 1 THEN i.name ELSE '' END,
ik.keyno, colname = c.name,
isdesc = CASE indexkey_property(o.id, i.indid, ik.keyno,
'IsDescending')
WHEN 1 THEN 'DESC'
ELSE ''
END
FROM sysobjects o
JOIN sysindexes i ON o.id = i.id
JOIN sysindexkeys ik ON i.id = ik.id
AND i.indid = ik.indid
JOIN syscolumns c ON c.id = ik.id
AND c.colid = ik.colid
WHERE i.indid = 1
ORDER BY o.name, ik.keyno

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Sep 1 '05 #5
Stu
Ahhhh; I didn't even see the sysindexkeys table. I tried doing it with
INDEX_COL(), but it was a miserable failure.

Stu

Sep 1 '05 #6
thanx guys. the pointer towards the right direction is much appreciated

bala

Sep 8 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve_CA | last post by:
Hi, The more I read, the more confused I'm getting ! (no wonder they say ignorance is bliss) I just got back from the bookstore and was flipping through some SQL Server Administration...
5
by: jim_geissman | last post by:
One table I manage has a clustered index, and it includes some varchar columns. When it is initially created, all the columns in the clustered index are populated, and then some of the longer...
2
by: Fred | last post by:
Let's say I suddenly discover that an unclustered table would benefit if it was clustered on column(s) already indexed. Currently, I'd need to drop that perfectly good index just to re-create it as...
1
by: anonieko | last post by:
A lot of detailed discussion explains the difference between clustered and non-clustered indexes. But very few 'clarifies' why the term used is 'clustered'. Well, once and for all, this is my...
2
by: Lyle Fairfield | last post by:
'Property Clustered As Boolean 'Member of DAO.Index Private Sub IsThereaClusteredIndex() Dim tdf As DAO.TableDef Dim idx As DAO.Index For Each tdf In DBEngine(0)(0).TableDefs For Each idx In...
5
by: pb648174 | last post by:
I've been doing a bit of reading and have read in quite a few places that an identity column is a good clustered index and that all or at least most tables should have a clustered index. The tool I...
1
by: Curt | last post by:
What is the difference please?
4
by: codefragment | last post by:
Hi I thought that given a table with an index, primary key and clustered index any non clustered index look ups would go via the clustered index and the primary key is irrelevant? (sql server...
0
by: Tawfiq | last post by:
Hi, I have got the following situation please give me some ideas how to solve/work around it. Current situation: Everyday day about 10 million records are processed and bulk inserted in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.