INDEX_COL (Transact-SQL)
SQL Server 2008 R2
Returns the indexed column name. Returns NULL for XML indexes.
Returns NULL on error or if a caller does not have permission to view the object.
A user can only view the metadata of securables that the user owns or on which the user has been granted permission. This means that metadata-emitting, built-in functions such as INDEX_COL may return NULL if the user does not have any permission on the object. For more information, see Metadata Visibility Configuration and Troubleshooting Metadata Visibility.
A. Using INDEX_COL to return an index column name
The following example returns the column names of the two key columns in the index PK_SalesOrderDetail_SalesOrderID_LineNumber.
USE AdventureWorks2008R2;
GO
SELECT
INDEX_COL (N'AdventureWorks2008R2.Sales.SalesOrderDetail', 1,1) AS
[Index Column 1],
INDEX_COL (N'AdventureWorks2008R2.Sales.SalesOrderDetail', 1,2) AS
[Index Column 2]
;
GO
Here is the result set:
Index Column 1 Index Column 2 ----------------------------------------------- SalesOrderID SalesOrderDetailID
