Solved

proc that runs any sql

Posted on 2002-06-05
2
289 Views
Last Modified: 2012-06-21
How would I write a proc that'll run any sql I send it, i.e. exec run_sql("select * from table where field = 'hello world'")?
0
Comment
Question by:arichexp
2 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7058365
Did you try:
Create Procedure sp_DynamicSQL @SQL varchar(1000)

As

Exec @SQL

Anthony
0
 
LVL 2

Accepted Solution

by:
bronwyn_black earned 100 total points
ID: 7058609
There is already a proc to do this in the master database.

you can call it with :

---
sp_sqlexec 'SELECT * FROM pubs..Authors'
---
Very similar to what acperkins suggested, it looks like this:
---
create procedure sp_sqlexec
    @p1 text
as
exec(@p1)
---
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

791 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