Joe Webb

Musing and observations about SQL Server, other technogies, and sometimes just life in general
posts - 85, comments - 401, trackbacks - 0

My Links

News

This blog has moved!

Click here for the new location.

Follow me on Twitter
Add to Technorati Favorites

Search this Blog
 




Archives

Post Categories

About me

The undocumented sp_MSforeachdb procedure

Late last year, I posted a blog that described the undocumented sp_MSforeachtable stored procedure in Microsoft SQL Server and how it could be used to execute commands on each table within a database. The examples I gave were executing DBCC CHECKTABLE and EXECUTE sp_spaceused() for each table in a given database.

But there's another undocumented stored procedure designed for iteration. This one allows us to iterate through each database in a SQL Server instance. It's called sp_MSforeachdb.

It's usage is very similar to that of sp_MSforeachtable; simply execute the stored procedure, passing it the command that you'd like to execute as a string. For example, to see the space consumed by each database, run the following command.

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_spaceused'

Notice that [?] is used as a placeholder for the heretofore unspecified database name. The results can be seen below.

sp_MSforeachdb1-2008-08-27

As another example, you can execute sp_helpfile on each database by running the following command.

EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC sp_helpfile'

 

sp_MSforeachdb2-2008-08-27

Of course undocumented implies unsupported and that there is no guarantees about whether the procedure will exist in future versions of the product. So, use at your own risk and discretion.

Got any favorite uses for sp_MSforeachdb? Feel free to share in the comments below.

Cheers!

Joe

kick it on DotNetKicks.com

Print | posted on Wednesday, August 27, 2008 5:28 PM | Filed Under [ T-SQL SQL Server ]

Feedback

Gravatar

# re: The undocumented sp_MSforeachdb procedure

EXEC sp_MSforeachdb
@command1='use ?; exec sp_changedbowner ''sa''' -- Change all DBs to "sa" owner

EXEC sp_MSforeachdb
@command1='ALTER DATABASE ? SET PAGE_VERIFY CHECKSUM' -- SQL 2005 Best Practice

EXEC sp_MSforeachdb
@command1='?.dbo.sp_change_users_login ''Report''' -- Check for Orphans



10/1/2008 1:30 AM | Peter B
Gravatar

# re: The undocumented sp_MSforeachdb procedure

GREAT SCRIPT! I couldn't remember the name of this proc and I needed it to update all stats on server in all databases. I appreciate the free help! :)
PS: found this info via google.
12/2/2009 6:42 AM | Kristina Rotach
Gravatar

# re: The undocumented sp_MSforeachdb procedure

the first one gives an error if you don't allow system table updates.

Msg 15109, Level 16, State 1, Line 1
Cannot change the owner of the master, model, tempdb or distribution database.

9/2/2010 3:17 PM | erh

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET