Executing a Query For Each Database in SQL Server

Mert Ilis
1 min readApr 13, 2016

--

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.

--

--

Mert Ilis
Mert Ilis

Written by Mert Ilis

I’m a software development enthusiast who likes trying different web technologies and adding value to his team.

No responses yet