I have a table with 10 columns as col_1,col_2,…. col_10. I want to write a select statement that will select a value of one of the row and from one of these 10 columns. I have a variable that will decide which column to select from. Can such query be written where the column name is dynamically decided from a variable.
Share
Since I don’t have enough details, I can’t give code. Instead, I’ll explain.
Declare a string variable, something like:
Set that variable to be the completed SQL string you want (as a string, and not actually querying… so you embed the row-name you want using string concatenation).
Then call:
exec(@sql)All set.