Re: Execute SQL Statment

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Buddy Ackerman (buddy_nospam_at_buddyackerman.com)
Date: 04/19/04


Date: Mon, 19 Apr 2004 09:08:12 -0500

Place 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
>



Relevant Pages

  • Execute SQL Statment
    ... Best Regards ... > fetch next from dbname_cur into @dbname ... >Now i need to execute this output. ...
    (microsoft.public.sqlserver.server)
  • Execute SQL Statment
    ... I need to execute "SQL Statments" that belong to a script ... declare @sqlstring as varchar(500 ... fetch next from dbname_cur into @dbname ...
    (microsoft.public.sqlserver.server)
  • Re: Execute SQL Statment
    ... instead of or right after print @dbname. ... SQL Server MVP ... > I need to execute "SQL Statments" that belong to a script ... > declare dbname_cur cursor for select from ...
    (microsoft.public.sqlserver.server)
  • DB name as parameter
    ... This depends on the code you are trying to execute. ... DECLARE @DBNAME varchar ... >(the EXEC command is not a good solution) ...
    (microsoft.public.sqlserver.programming)
  • Bind Variable in CURSOR
    ... I have a CURSOR which has a variable in the WHERE clause: ... WHERE dbname = @dbname ... FETCH NEXT FROM get_tabs ... SELECT @runindcursor = 1 ...
    (comp.databases.ms-sqlserver)