SQL Server – sp_msforeachdb undocumented stored procedure with simple example

Sp_msforeachdb can be handy to use as option to perform maintenance task for each database in sql server. sp_msforeachdb is undocumented stored procedure and not supported by Microsoft.  Sp_msforeachdb will help you to iterate through all database and execute your sql command.  Sp_msforeachdb  may be discontinued in future version of sql server.

DBA use is to monitor the database growth rate of all database to fulfill the space requirement of future on sql server instance.

We can use sp_spaceused command to monitor database size.

Here is the simple example, How we can use sp_msforeachdb stored procedure to gather database size information in sql server.

exec sp_msforeachdb ‘USE [?];  sp_spaceused’


We can schedule this task for monthly basis to capture the database growth information.

My recommendation is not to use sp_msforeachdb undocumented stored procedure for critical activity.

Leave a Reply




7 − 2 =