Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are multiple ways to retrieve column names from a select query in SQL Server:

  1. Using the INFORMATION_SCHEMA.COLUMNS table:
SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'table_name'

Replace 'table_name' with the name of the table you want to retrieve the column names from.

  1. Using the sys.columns table:
SELECT name 
FROM sys.columns 
WHERE object_id = OBJECT_ID('table_name')

Replace 'table_name' with the name of the table you want to retrieve the column names from.

  1. Using the sp_columns stored procedure:
EXEC sp_columns 'table_name'

Replace 'table_name' with the name of the table you want to retrieve the column names from.