Often we may be required to get the names of all columns in a table in SQL server. In this post we will see the different options available in SQL server to fetch the column names.
1) We can use the below query to get the column names.
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table Name'
ORDER BY ORDINAL_POSITION
'Table Name' needs to be replaced by the actual table name , for example if table name is Employee we need to modify the query as below:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employee'
ORDER BY ORDINAL_POSITION
2) We can also use a System Stored Procedure sp_columns also and it will provide some additional information
EXEC sp_columns 'Table Name'
'Table Name' needs to be replaced by the actual table name , for example if table name is Employee we need to modify the query as below:
EXEC sp_columns 'Employee'
0 Comment(s)