|
Posted by Erland Sommarskog on 04/19/07 21:51
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/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
[Back to original message]
|