Comprehensive Index Information

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    Comprehensive Index Information

    Hi,

    I am writing an in house utility to attempt to compare different
    aspects of databases.
    I am currently writing the queries to list all of the indexes in the
    database (including primary key indexes at present - I may move these
    and compare separately at some point).

    I would like the following information, in one result set if possible:

    Table Name
    Index Name
    Column Name
    Column Position
    Unique?

    Now on Oracle, this is easily done with the following query:

    SELECT IND.TABLE_NAME, IND.INDEX_NAME, IND.COLUMN_NAME ,
    IND.COLUMN_POSI TION, COL.UNIQUENESS
    FROM USER_IND_COLUMN S IND,
    USER_INDEXES COL
    WHERE IND.INDEX_NAME = COL.INDEX_NAME
    ORDER BY 1, 2, 3, 4, 5

    I have been trying for over an hour now to get the equivalent, and I
    really cannot figure it out. If anybody can come up with this then I
    would greatly appreciate it!

    Many Thanks,

    Paul

  • Erland Sommarskog

    #2
    Re: Comprehensive Index Information

    Paul (paulwragg2323@ hotmail.com) writes:
    I am writing an in house utility to attempt to compare different
    aspects of databases.
    Before you go too far, pay a visit to http://www.red-gate.com and
    if SQL Compare meets your needs.
    I am currently writing the queries to list all of the indexes in the
    database (including primary key indexes at present - I may move these
    and compare separately at some point).
    >
    I would like the following information, in one result set if possible:
    >
    Table Name
    Index Name
    Column Name
    Column Position
    Unique?
    SELECT tablename = t.name, indexname = i.name,
    colname = c.name, pos = ic.index_column _id,
    indextype = i.type_desc, isunique = i.is_unique
    FROM sys.tables t
    JOIN sys.indexes i ON t.object_id = i.object_id
    JOIN sys.index_colum ns ic ON i.object_id = ic.object_id
    AND i.index_id = ic.index_id
    JOIN sys.columns c ON t.object_id = c.object_id
    AND ic.column_id = c.column_id
    WHERE i.is_hypothetic al = 0

    There are probably more columns should include in the output, but I
    levae that as an exercise.

    Note: the above works in SQL 2005 only. Next time, please specify which
    version of SQL Server you are using.


    --
    Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

    Books Online for SQL Server 2005 at

    Books Online for SQL Server 2000 at

    Comment

    • Paul

      #3
      Re: Comprehensive Index Information

      Hi Erland,

      Thankyou very much for this. Of course, as usual I stupidly forgot to
      post the version. Sorry about that. Really I need something that will
      work on both SQL Server 2000 and SQL Server 2005.

      Thanks for the link - unfortunately this is more of an exercise for
      the time being and so we are not willing to spend money on a tool at
      present!

      Thanks for the help - if you do know something that will work on both
      versions that would be good.

      Paul

      Comment

      • Erland Sommarskog

        #4
        Re: Comprehensive Index Information

        Paul (paulwragg2323@ hotmail.com) writes:
        Thankyou very much for this. Of course, as usual I stupidly forgot to
        post the version. Sorry about that. Really I need something that will
        work on both SQL Server 2000 and SQL Server 2005.
        Then you need to work against sysobjects, sysindexes, sysindexkeys and
        syscolumns. The query will be similar, but you need to filter for
        statistics, since in SQL 2000 statistics and indexes live in sysindexes.

        These are documented in Books Online, and since this is an exercise for you,
        I leave you there. :-)


        --
        Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

        Books Online for SQL Server 2005 at

        Books Online for SQL Server 2000 at

        Comment

        • Paul

          #5
          Re: Comprehensive Index Information


          Thanks Erland.

          Comment

          Working...