Wednesday, March 7, 2012

Execute SQL Statment

Hello,
I need to execute "SQL Statments" that belong to a script
but i think that im not doing it in the right way.
I only can print the output, but i dont know how to
execute it.
I've th following code:
if (select count(1) from master..sysdatabases) > 0
declare dbname_cur cursor for select [name] from
master..sysdatabases
declare @.dbname as varchar(500)
declare @.sqlstring as varchar(500
)
open dbname_cur
fetch next from dbname_cur into @.dbname
WHILE @.@.FETCH_STATUS = 0
begin
set @.dbname = 'select [name],[filename] from ' + @.dbname
+ '..sysfiles'
print @.dbname
fetch next from dbname_cur into @.dbname
end
close dbname_cur
deallocate dbname_cur
This code give me the following output:
select [name],[filename] from example..sysfiles
select [name],[filename] from master..sysfiles
select [name],[filename] from model..sysfiles
select [name],[filename] from msdb..sysfiles
select [name],[filename] from Northwind..sysfiles
select [name],[filename] from pubs..sysfiles
select [name],[filename] from tempdb..sysfiles
Now i need to execute this output.
Best regardsPlace the follwoing code after the "print @.dbname" line in your cursor loop:
exec (@.dbname)
--Buddy
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>|||Use:
EXEC (@.dbname)
instead of or right after print @.dbname.
--
Jacco Schalkwijk
SQL Server MVP
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>|||After print @.dbname, add
exec (@.dbname)
Regards,
Paul Ibison|||Thanks everybody,
Best Regards
>--Original Message--
>Hello,
>I need to execute "SQL Statments" that belong to a script
>but i think that im not doing it in the right way.
>I only can print the output, but i dont know how to
>execute it.
>I've th following code:
>if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
>master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
>)
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' +
@.dbname
>+ '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
>close dbname_cur
>deallocate dbname_cur
>This code give me the following output:
>select [name],[filename] from example..sysfiles
>select [name],[filename] from master..sysfiles
>select [name],[filename] from model..sysfiles
>select [name],[filename] from msdb..sysfiles
>select [name],[filename] from Northwind..sysfiles
>select [name],[filename] from pubs..sysfiles
>select [name],[filename] from tempdb..sysfiles
>Now i need to execute this output.
>Best regards
>.
>

No comments:

Post a Comment