I want to ask you if is possible to declare a constant in SQL Server which provides the column name ?
means something like this :
declare @column varchar(30)
set @column = 'Column1'
SELECT @column FROM Table1
I know that above declaration is wrong but maybe is a solution to provide yourself the column name.
I want to show results for Column1 but if Column1 is inside ” then will return with (No column name) additional column name and the results on all rows will be Column1.
Thank you
You can’t use dynamic column names in SQL.
To do so, you need to use dynamic SQL. I suggest reading The Curse and Blessings of Dynamic SQL by Erland Sommarskog for a complete view on this issue – the article is long but comprehensive.
The use of dynamic SQL can expose you to SQL injection, so use with care.