Sql Query stored as a string in a table

Hi experts,
Joping this is a straight-forward and well worn path...

Basically I wish to store a number of different SQL queries in a table (as individual strings). Then I hope to be able to call a specific query from the table and execute it.

How would I go about executing a query which is stored as a string in a field on a particular table? Appreciate any tutorials etc that you can point me to. I will be working in Microsoft Sql Server 2008.

Cheers, split_pin
Split_PinAsked:
Who is Participating?
 
Dale BurrellConnect With a Mentor DirectorCommented:
declare @Sql nvarchar(max)

select @Sql = MySqlString from MyTable where id = @Id

execute(@Sql)
0
 
HuaMin ChenConnect With a Mentor System AnalystCommented:
The way is to use Dynamic sql to call the query that is stored in the table.

You can also use a cursor to retrieve the stored column in the table.
0
 
Split_PinAuthor Commented:
Thanks both were helpful responses - apologies for my slow reply I was unable to access network for several days,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.