Pages - Menu

Friday, June 2, 2017

Get Column Names from a Table in SQL Server

Here, I will explain, how to get the list of column names using Table name as input. There are multiple ways to get the column names from a table. You can get the column list with Select statement also, but there we will get data too.

If you want to get only list of columns from a table below are the queries which will work for you.

Option 1:

SELECT 
NAME AS [COLUMN NAME] 
FROM 
SYS.COLUMNS
WHERE 
OBJECT_NAME(OBJECT_ID) = @TABLE_NAME
ORDER BY 
COLUMN_ID

Option 2:

SELECT
    SC.NAME [COLUMN NAME]
FROM
    SYSCOLUMNS SC
    INNER JOIN SYSOBJECTS SO ON
    SO.ID = SC.ID
WHERE

    SO.NAME = @TABLE_NAME

Option 3:

you can get the column list from a table with their datatype information using below query.

SP_HELP @TABLE_NAME;



No comments:

Post a Comment