The sp_MSforeachdb Stored Procedure

The sp_MSforeachdb Stored Procedure

The sp_MSforeachdb stored procedure helps you to do repetitive actions against all or some of the SQL Server instance databases.

For example, this query:

EXEC sp_MSforeachdb 'USE [?]; EXEC sp_spaceused'

Displays the space used (Reserved space, Data, index Size, Unused Space) by each database.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist