Once in a while, a DBA or SQL developer is in the need of listing all the indexes of one or more tables in a given database.
Though the indexes can be viewed by using sp_help <table name> for a table, it does not help when it comes for multiple tables. Here the DBA's nice-to-have query is one to list Tables and Indexes.
Let's go and derive the query to list all the tables and their indexes.
For testing these queries in the test database, it is nice to have few tables created.
See the script for examples./
- sysobjects consists of all the objects in the database, while sysindexes has only index details.
Now, let's write the query in step by step fashion.
Query 1 - List all indexes in a database
Query 2 - Now let's exclude dtproperties table (indid = 255) and each tablename itself (indid =0) stored in sysindexes
Query 3 - List objects created by users
Query 4 - Let's build the complete query by joining both sysobjects and sysindexes.