From: Muhammad Bilal on
Hi

Let if I have a tb1 with col1

CREATE TABLE tbl1
(
Col1 VARCHAR(500)
)


INSERT INTO tbl1 VALUES('SELECT * FROM tbl1')
INSERT INTO tbl1 VALUES('SELECT Query FROM tbl1')

In col1 I have inseted a query as a string. How can I run the query stored
in col1.

Regards,
Muhammad Bilal

From: Plamen Ratchev on
You have to use dynamic SQL:
http://www.sommarskog.se/dynamic_sql.html

--
Plamen Ratchev
http://www.SQLStudio.com
From: John Bell on
On Fri, 30 Apr 2010 15:34:01 -0700, Muhammad Bilal
<MuhammadBilal(a)discussions.microsoft.com> wrote:

>Hi
>
>Let if I have a tb1 with col1
>
>CREATE TABLE tbl1
> (
> Col1 VARCHAR(500)
> )
>
>
>INSERT INTO tbl1 VALUES('SELECT * FROM tbl1')
>INSERT INTO tbl1 VALUES('SELECT Query FROM tbl1')
>
>In col1 I have inseted a query as a string. How can I run the query stored
>in col1.
>
>Regards,
>Muhammad Bilal

Hi Muhammad

You could do something like:

DECLARE @qry varchar(500)

DECLARE qry_crs CURSOR FOR
SELECT Col1 FROM tbl1

OPEN qry_crs

FETCH NEXT FROM qry_crs INTO @qry

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ( @qry )
FETCH NEXT FROM qry_crs INTO @qry
END
CLOSE qry_crs
DEALLOCATE qry_crs

You should also read http://www.sommarskog.se/dynamic_sql.html

John