This is a discussion on Comprehensive Index Information within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I am writing an in house utility to attempt to compare different aspects of databases. I am currently ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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_POSITION, COL.UNIQUENESS FROM USER_IND_COLUMNS 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 |
| |||
| 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_columns 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_hypothetical = 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@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |
| |||
| 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 |
| |||
| 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@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |