Ask Your Question
2

What is the method to retrieve the column names from a select query in SQL Server?

asked 2023-07-11 12:45:46 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-11 13:09:02 +0000

devzero gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-11 12:45:46 +0000

Seen: 7 times

Last updated: Jul 11 '23