Getting a Variable that has a SELECT statement to Return Results

I have concatenated a long Select Statement and assigned it to a variable. (i.e.)

@a = Select * from foo ---Obviously mine is much more robust

How do I execute @a to actually Select * from foo




Answer this question

Getting a Variable that has a SELECT statement to Return Results

  • Latif1

    Perfect. Thank you.

  • cicorias

    You can use

    exec (@a)


    If @a is nvarchar rather than varchar you can also do this

    exec sp_executesql @a


  • Getting a Variable that has a SELECT statement to Return Results