Sometimes we need to perform a specific task on all databases. Like to run a specific SP on all database. In that case if you have a number of databases (likely in most of cases) then an automated method is required rather than to perform the task on each database one by one. In SQL Server T-SQL we have a system stored procedure (sp) sp_msforeachdb for this purpose. For example if you want to check database integrity of all databases, then you may run the following command.
EXEC sp_msforeachdb 'DBCC CHECKDB()'
The command DBCC CHECKDB() will be executed for all databases one by one.