• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1008
  • Last Modified:

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
0
Split_Pin
Asked:
Split_Pin
2 Solutions
 
HuaMinChenBusiness 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
 
Dale BurrellCommented:
declare @Sql nvarchar(max)

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

execute(@Sql)
0
 
Split_PinAuthor Commented:
Thanks both were helpful responses - apologies for my slow reply I was unable to access network for several days,
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now