Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Exec multiple batches (GO) with dynamic sql

Author  Topic 

afaa
Starting Member

16 Posts

Posted - 2008-06-10 : 19:25:08
Hello SQL Team!
I'm stuck at this problem for days and need help.
The problem is with the GO keyword. I know the GO causes the batch to get executed and all local variables are lost. But I can't seem to find a work around. I would like each stored procedure to get executed in different batches.

create table sql_cmd(cmd nvarchar(255))
insert into sql_cmd(cmd) values('exec user_sp param1,''param2'')
insert into sql_cmd(cmd) values('exec user_sp2 param1,''param2'')

declare @sql nvarchar(255)
declare c_sql cursor --small table peformance not a problem. Also open to other suggestions
for select cmd from sqlcmd
open c_sql
fetch next from c_sql
into @sql
while @@fetch_status = 0
begin
print @sql
exec sp_executesql @sql
GO
fetch next from c_sql
into @sql
end

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-10 : 19:36:42
Remove the GO and you should be good. Dynamic SQL executes in its own batch already, so GO isn't needed to start a new one.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

afaa
Starting Member

16 Posts

Posted - 2008-06-10 : 19:48:28
"Dynamic SQL executes in its own batch already"

I did not know that. Thank you. Is there any documentation on that?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-10 : 19:53:24
I'm sure there is, but I don't have it handy.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-11 : 09:52:23
http://www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -