SQL Server – sp_MSforeachdb – Undocumented Stored Procedure
The undocumented stored procedure “sp_MSforeachdb” is found in the “master” database. This is similar to sp_MSforeachtable. This stored procedure Sp_MSforeachdb gives a DBA the ability to cycle through every database in your catalog. This stored procedure will loop every database in your catalog for performing a command. This stored procedure accepts the following input parameters.
Example:
You can run a DBCC CHECKDB on all databases as below
EXEC sp_Msforeachdb "DBCC checkdb ('?')"
Similarly you can run other command also.