Re: Looping through databases in stored proc
From: Keith Kratochvil (sqlguy.back2u_at_comcast.net)
Date: 02/03/05
- Next message: Paul: "RE: Compare Data in two differant tables"
- Previous message: Bob Barrows [MVP]: "Re: Looping through databases in stored proc"
- In reply to: Robert Richards via SQLMonster.com: "Looping through databases in stored proc"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 3 Feb 2005 14:04:04 -0600
Yuu could try declaring and executing a string within your stored procedure
(I assume that you are using a cursor):
declare @str varchar(255)
set @str = 'exec ' + @dbname + '..sp_helpfile'
--optional
select @str AS TheStringToExecute
exec (@str)
or you can use the undocumented stored procedure that is shown below:
EXEC sp_Msforeachdb 'PRINT (''?''); EXEC sp_helpfile'
-- Keith "Robert Richards via SQLMonster.com" <forum@SQLMonster.com> wrote in message news:76dfff48350745e79fe6fe1024555bb9@SQLMonster.com... > I am trying to loop through the databases on a server (SQL 2000) and > dynamically run sp_helpfile against each database on the server. Of course > that means I need to store the name of the database as a variable or > parameter. > > When I use the following code I am told "a USE database statement is not > allowed in a procedure or trigger.": > > use @dbname > go > exec sp_helpfile > > When I use the following code I am told "Incorrect syntax near '.'" > exec @dbname..sp_helpfile > > Any suggestions? > > -- > Message posted via http://www.sqlmonster.com
- Next message: Paul: "RE: Compare Data in two differant tables"
- Previous message: Bob Barrows [MVP]: "Re: Looping through databases in stored proc"
- In reply to: Robert Richards via SQLMonster.com: "Looping through databases in stored proc"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|