Skip to content

selecting a variable string name in sql select query in case of stored procedures

Suppose I want to select a constant value in an sql query, I could do something like this :

I want to dynamically generating an sql query using stored procedures. For example :

Where @TableName, @Scname are dynamically generated variables in a while loop. Here @Scname is interpreted as a column name instead of a constant string when executing query on @TableName. I have tried using escape characters as follows :

But it returns a floating point value full of zeros instead of the string contained in @Scname.

Advertisement

Answer

I think following should work

User contributions licensed under: CC BY-SA
6 People found this is helpful