COL_NAME (Transact-SQL)

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

Returns the name of a column from a specified corresponding table identification number and column identification number.

Topic link icon Transact-SQL Syntax Conventions

Syntax

COL_NAME ( table_id , column_id )  

Arguments

table_id
Is the identification number of the table that contains the column. table_id is of type int.

column_id
Is the identification number of the column. column_id parameter is of type int.

Return types

sysname

Exceptions

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 COL_NAME may return NULL if the user does not have any permission on the object. For more information, see Metadata Visibility Configuration.

Remarks

The table_id and column_id parameters together produce a column name string.

For more information about obtaining table and column identification numbers, see OBJECT_ID (Transact-SQL).

Examples

The following example returns the name of the first column in a sample Employee table.

-- Uses AdventureWorks  

SELECT COL_NAME(OBJECT_ID('dbo.FactResellerSales'), 1) AS FirstColumnName,  
COL_NAME(OBJECT_ID('dbo.FactResellerSales'), 2) AS SecondColumnName;  

Here is the result set.

ColumnName          
------------   
BusinessEntityID  

See also

Expressions (Transact-SQL)
Metadata Functions (Transact-SQL)
COLUMNPROPERTY (Transact-SQL)
COL_LENGTH (Transact-SQL)