From: mutasie on
Hello,

I would like to store multiple dynamic SQL in some meta table and use
a cursor to read selected dynamic sql and execute to increase
flexibility. Is this even possible? See code example below:...



declare @csql nvarchar(4000)

DECLARE c1 CURSOR READ_ONLY
FOR
SELECT sql
FROM dbo.AH_Test_Meta
OPEN c1

FETCH NEXT FROM c1
INTO @csql

WHILE @@FETCH_STATUS = 0
BEGIN

EXEC sp_executesql @csql

FETCH NEXT FROM c1
INTO @csql

END
CLOSE c1
DEALLOCATE c1
From: Uri Dimant on
You can execute it but permit me to ask WHY?

DECLARE @v VARCHAR(50);
SET @v = 'N' + '''' + 'select getdate()' + '''';
EXEC ('EXEC sp_executesql ' + @v);





"mutasie" <mutasie(a)gmail.com> wrote in message
news:59ee5325-93c1-49e4-88c5-89ddf21abf51(a)s21g2000prm.googlegroups.com...
> Hello,
>
> I would like to store multiple dynamic SQL in some meta table and use
> a cursor to read selected dynamic sql and execute to increase
> flexibility. Is this even possible? See code example below:...
>
>
>
> declare @csql nvarchar(4000)
>
> DECLARE c1 CURSOR READ_ONLY
> FOR
> SELECT sql
> FROM dbo.AH_Test_Meta
> OPEN c1
>
> FETCH NEXT FROM c1
> INTO @csql
>
> WHILE @@FETCH_STATUS = 0
> BEGIN
>
> EXEC sp_executesql @csql
>
> FETCH NEXT FROM c1
> INTO @csql
>
> END
> CLOSE c1
> DEALLOCATE c1


From: TheSQLGuru on
This can be done. I have several clients that have systems like this.

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"mutasie" <mutasie(a)gmail.com> wrote in message
news:59ee5325-93c1-49e4-88c5-89ddf21abf51(a)s21g2000prm.googlegroups.com...
> Hello,
>
> I would like to store multiple dynamic SQL in some meta table and use
> a cursor to read selected dynamic sql and execute to increase
> flexibility. Is this even possible? See code example below:...
>
>
>
> declare @csql nvarchar(4000)
>
> DECLARE c1 CURSOR READ_ONLY
> FOR
> SELECT sql
> FROM dbo.AH_Test_Meta
> OPEN c1
>
> FETCH NEXT FROM c1
> INTO @csql
>
> WHILE @@FETCH_STATUS = 0
> BEGIN
>
> EXEC sp_executesql @csql
>
> FETCH NEXT FROM c1
> INTO @csql
>
> END
> CLOSE c1
> DEALLOCATE c1


From: --CELKO-- on
The short answer is yes, you can.

The right answer is why would you do that? Dynamic SQL is bad enough
-- it tells us that you have no idea what you are doing until someone
decides at run time. Then there is SQL Injection, poor performance,
etc.

Then you have decided to implement this bad idea with cursors, which
are also a bad programming practice. I will not even comment on
mixing data and meta-data in a schema.
From: Steve on
Why wouldn't you just create a procedure out of it then?





"mutasie" wrote:

> Hello,
>
> I would like to store multiple dynamic SQL in some meta table and use
> a cursor to read selected dynamic sql and execute to increase
> flexibility. Is this even possible? See code example below:...
>
>
>
> declare @csql nvarchar(4000)
>
> DECLARE c1 CURSOR READ_ONLY
> FOR
> SELECT sql
> FROM dbo.AH_Test_Meta
> OPEN c1
>
> FETCH NEXT FROM c1
> INTO @csql
>
> WHILE @@FETCH_STATUS = 0
> BEGIN
>
> EXEC sp_executesql @csql
>
> FETCH NEXT FROM c1
> INTO @csql
>
> END
> CLOSE c1
> DEALLOCATE c1
>