Well it so happens that there is an undocumented stored procedure that you can use to do so and has been in SQL Server for some time now.  This will definitely be handy if you are a software developer that does not like to code with cursor in SQL.  I tested this in SQL Server 2008 R2 and the stored procedure is still there.  Remind you that this stored procedure is undocumented and probably not supported and thus might not exist in future versions of SQL Server.  And there is also a version for table as well.  Pretty cool eh.  See sample usage below.

-- execute the following command on each database EXEC sp_MSforeachdb ‘PRINT ‘’?’’’

-- execute the following command on each table in the database USE MyDatabase EXEC sp_MSforeachtable ‘PRINT ‘’?’’’;

-- the following uses a filter -- it makes sure command will not execute on master, tempDB, model, and msdb EXEC sp_MSforeachdb ‘ IF ‘’?’’ NOT IN (‘‘master’’, ‘‘tempDB’’, ‘‘model’’, ‘‘msdb’’) BEGIN PRINT ‘’?’’ END’

 

 

Below are links to more information: