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.
Below are links to more information:
- The undocumented sp_MSforeachdb procedure
- The undocumented sp_MSforeachtable procedure
- SQL Server: Applying Filter on sp_MSforeachDB