Solved

Sql Query stored as a string in a table

Posted on 2014-01-01
3
874 Views
Last Modified: 2014-01-05
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
Comment
Question by:Split_Pin
3 Comments
 
LVL 10

Assisted Solution

by:HuaMinChen
HuaMinChen earned 250 total points
ID: 39750803
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
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 250 total points
ID: 39750838
declare @Sql nvarchar(max)

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

execute(@Sql)
0
 

Author Closing Comment

by:Split_Pin
ID: 39757980
Thanks both were helpful responses - apologies for my slow reply I was unable to access network for several days,
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now