The short answer is yes.
There is an undocumented stored procedure sp_msforeachdb that will iterate through all databases in the instance. a simple example:
exec sp_msforeachdb 'Select ?'
will list all databases (database name is substituted where ever
for your purposes you could write something like:
declare @command varchar(2000)
set @command = 'Use ?
-- do stuff
'
exec sp_msforeachdb @command
Personally, I would putting all my queries in a stored procedure and
doing something like:
exec sp_msforeachdb 'exec my_sp ?'
Then I have lots of flexibility, can write dynamic sql, use control logic etc.
Main Topics
Browse All Topics





by: RiteshShahPosted on 2009-10-28 at 22:32:43ID: 25690902
seems like you have to go for undocumented stored procedure SP_MsForEachDB which will iterate through databases available in your instance. look at my following blog link:
/2009/03/s pmsforeach db-undocum ented- stor ed.html
http://www.sqlhub.com