Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
This function returns the name of a table column, based on the table identification number and column identification number values of that table column.
Transact-SQL syntax conventions
Syntax
COL_NAME ( table_id , column_id )
Arguments
table_id
The identification number of the table containing that column. The table_id argument has an int data type.
column_id
The identification number of the column. The column_id argument has an int data type.
Return types
sysname
Exceptions
Returns NULL
on error, or if a caller doesn't have the correct permission to view the object.
A user can only view the metadata of securables that the user owns, or on which the user is granted permission. This means that metadata-emitting, built-in functions such as COL_NAME
might return NULL
, if the user doesn't have correct permissions 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.
Examples
The code samples in this article use the AdventureWorks2022
or AdventureWorksDW2022
sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page.
A. Return names of first two columns in a table
This example returns the name of the first two columns in the Person.Person
table.
USE AdventureWorks2022;
GO
SELECT COL_NAME(OBJECT_ID('Person.Person'), 1) AS FirstColumnName,
COL_NAME(OBJECT_ID('Person.Person'), 2) AS SecondColumnName;
Here's the result set.
FirstColumnName SecondColumnName
----------------- -----------------
BusinessEntityID PersonType