Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 117,621 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,977 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Columns having indexes in sql server 2000

 
Reply to this topicStart new topic

Columns having indexes in sql server 2000, DETERMINE INDEX COLUMNS

Avaneesh Bajoria
post 29 Apr, 2008 - 04:07 AM
Post #1


New D.I.C Head

*
Joined: 28 Apr, 2008
Posts: 2

I need to know which columns in my table have indexes, i.e I want their names I have used the following :
CODE
select name from sysindexes where id = object_id('tablename')

CODE
SELECT OBJECTPROPERTY(OBJECT_ID('tablename'), 'tablehasindex')
User is offlineProfile CardPM

Go to the top of the page


baavgai
post 29 Apr, 2008 - 05:40 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,763



Thanked 70 times

Dream Kudos: 400

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


You've got one of the data dictionary tables. There are more. This should get you what you need:

CODE

SELECT so.name table_name,
        si.name AS index_name,
        sik.indid, sik.colid, sik.keyno,
        sc.name AS column_name
    FROM sysobjects so
            INNER JOIN sysindexes si
                ON so.id = si.id
            INNER JOIN sysindexkeys sik
                ON si.id = sik.id
                    AND si.indid = sik.indid
            INNER JOIN syscolumns sc
                ON sik.id = sc.id
                    AND sik.colid = sc.colid
    WHERE so.xtype='U'
        AND so.name='tablename'
    ORDER BY so.name, si.name, sik.keyno


Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

Avaneesh Bajoria
post 29 Apr, 2008 - 09:56 AM
Post #3


New D.I.C Head

*
Joined: 28 Apr, 2008
Posts: 2

QUOTE(baavgai @ 29 Apr, 2008 - 05:40 AM) *

You've got one of the data dictionary tables. There are more. This should get you what you need:

CODE

SELECT so.name table_name,
        si.name AS index_name,
        sik.indid, sik.colid, sik.keyno,
        sc.name AS column_name
    FROM sysobjects so
            INNER JOIN sysindexes si
                ON so.id = si.id
            INNER JOIN sysindexkeys sik
                ON si.id = sik.id
                    AND si.indid = sik.indid
            INNER JOIN syscolumns sc
                ON sik.id = sc.id
                    AND sik.colid = sc.colid
    WHERE so.xtype='U'
        AND so.name='tablename'
    ORDER BY so.name, si.name, sik.keyno


Hope this helps.


Thanks you so much for your early reply dear. I did get my answer through your query, but I used the system stored procedure sp_helpindex 'tablename' and it fetched me exactly what I wanted. Once again thanks a lot. smile.gif

"Life's never too tough or easy only we make it so..."
User is offlineProfile CardPM

Go to the top of the page

JulieBunavicz
post 29 Apr, 2008 - 12:48 PM
Post #4


New D.I.C Head

*
Joined: 27 Apr, 2008
Posts: 6

Ahh sp_helpindex I'm practicing DDL.... Thanks for the tip.

Regards,

Julie Bunavicz
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 29 Apr, 2008 - 03:43 PM
Post #5


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,763



Thanked 70 times

Dream Kudos: 400

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


You know, as I was writing the query, I thought I could probably just use some variant of sp_help. However, in the time it would take me to figured out the syntax; query done.

I'll almost always prefer the SQL option, actually. If you're writing your own DDL scripts, getting output formatted in precisely the way you need is invaluable. Also, in addition to sp_help and the sysXX tables, there's another option using the views owned by information_schema.

Happy to help.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/8/08 12:17AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month