Run query on multiple databases or tables sp_msforeachdb or sp_msforeachtable

I had been using sp_msforeachdb for long to search any object (table, column, Procedure, etc) on a given SQL instance. I come across similar SP sp_msforeachtable which can be used to run against all tables.

Example:

sp_msforeachtable 'execute(''select "?", COUNT(*) from ?'')'
GO
sp_msforeachtable 'select "?"'
Advertisements
This entry was posted in SQL, SQL Query and tagged , , , , , , . Bookmark the permalink.

2 Responses to Run query on multiple databases or tables sp_msforeachdb or sp_msforeachtable

  1. Pingback: Find the Database where specific Procedure/table exists in a given SQL instance | Blog for reference – Vijred

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s