If you want to execute an SQL query for each database in your SQL Server you can use the “ sp_msforeachdb “ method.
For example,
EXECUTE sp_msforeachdb 'print ''[?]'''
In the above example the name of the databases in your SQL Server will be printed to the Messages tab including the system databases.
Result:
[master]
[tempdb]
[model]
[msdb]
[ReportServer$MyData]
…
Hope it helps!
Originally published at https://www.weboideas.com on April 13, 2016.