From: Muhammad Bilal on 30 Apr 2010 18:34 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 30 Apr 2010 19:07 You have to use dynamic SQL: http://www.sommarskog.se/dynamic_sql.html -- Plamen Ratchev http://www.SQLStudio.com
From: John Bell on 1 May 2010 04:44 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
|
Pages: 1 Prev: BCP Qusetion Next: US-MD-Silver Spring: SQL VMWare TV Network DBA |