Re: Execute SQL Statment

From: Jacco Schalkwijk (NOSPAMjaccos_at_eurostop.co.uk)
Date: 04/19/04


Date: Mon, 19 Apr 2004 15:10:02 +0100

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
>


Relevant Pages

  • 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: Find The Users and their roles for all the databases on the server
    ... SQL Server MVP ... "MANCPOLYMAN" wrote in message ... > can anyone point me in the right direction of how to loop through> sysdatabases, take each dbname and the use thatname.sysusers to display> all ... > fist cursor, of dbnames. ...
    (microsoft.public.sqlserver.programming)
  • 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)
  • Re: Alter identity -field?
    ... replace the dbname and table name with actual ... DBCC CHECKIDENT (tablename, RESEED, 4000) ... SQL Server MVP ... > I have a table with int identity field ). ...
    (microsoft.public.sqlserver.clients)
  • Re: How to know if a table exist in a Stored Procedure?
    ... use dbname ... Select object_namefrom syscomments where text like '%table_name%' ... SQL Server MVP ... Jorge L. Cotarelo ...
    (microsoft.public.sqlserver.msde)