Monday, September 9, 2024

How do I view all indexes on a table in SQL Server?

 You can use the following query to list all indexes on a specific table:

SELECT i.name AS IndexName, i.type_desc AS IndexType, c.name AS ColumnName FROM sys.indexes i INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id WHERE i.object_id = OBJECT_ID('YourTableName');

Replace 'YourTableName' with the actual table name. This query returns all indexes and their corresponding columns for the specified table.