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
[quoted text, click to view] "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
Look at the "Undocumented" stored procedure sp_MSforeachdb in the Master
database.
Don't forget, use at your own risk. Since it is undocumented, it may change
or disappear in the next version or service pack. If this is a one-time
thing, go ahead, but don't use in production code.
[quoted text, click to view] "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
[quoted text, click to view] Robert Richards via SQLMonster.com wrote:
> 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?
>
http://www.sommarskog.se/dynamic_sql.html Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.