Click here to Skip to main content
12,820,546 members (29,361 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 16 Sep 2012

SQL Server: Applying Filter on sp_MSforeachDB

, 16 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to apply filter on sp_MSforeachDB


While working on multiple databases on a single instance, sometimes, you need to execute a query for each database for which sp_MSforeachdb is the best choice.

Recently talking to my development team, I came to know that very few guys have an idea about a filter for sp_MSforeachDB.

For example, if I need to get database physical files information for each database on my instance, I will use the following simple query:

EXEC sp_MSforeachdb '
       SELECT name,physical_name,state,size
       FROM ?.sys.database_files

BUT what if I need to omit MSDB, TempDB and Model databases for this query. Now I have to apply a filter. This can be achieved by a simple IF statement.

EXEC sp_MSforeachdb 'IF ''?''  NOT IN (''tempDB'',''model'',''msdb'')
       SELECT name,physical_name,state,size
       FROM ?.sys.database_files

You can even use ? sign in WHERE clause.

EXEC sp_MSforeachdb 'IF ''?''  NOT IN (''tempDB'',''model'',''msdb'')
       SELECT name,physical_name,state,size
       FROM ?.sys.database_files
       WHERE name  LIKE ''?%'' -- Only Files starting with DB name

Output can be saved in tables (user, temporary) or table variables:

DECLARE   @DatabasesSize TABLE
      name VARCHAR(50),
      physical_name VARCHAR(500),
      state BIT,
      size INT
INSERT  INTO@DatabasesSize
EXEC sp_MSforeachdb 'IF ''?''  NOT IN (''tempDB'',''model'',''msdb'')
       SELECT name,physical_name,state,size
       FROM ?.sys.database_files


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

aasim abdullah
Team Leader CureMD
Pakistan Pakistan
Aasim Abdullah is working as SQL Server DBA with CureMD ( based in NY, USA. He has been working with SQL Server since 2007 (Version 2005) and has used it in many projects as a developer, administrator, database designer. Aasim's primary interest is SQL Server performance tuning. If he finds the time, he like to sketch faces with graphite pencils.

You may also be interested in...


Comments and Discussions

QuestionExcellent scripts Pin
tgrignon19-Feb-14 5:44
membertgrignon19-Feb-14 5:44 
QuestionCaveat in using sp_MSforeachdb Pin
AcidRaZor7-Jan-13 3:42
memberAcidRaZor7-Jan-13 3:42 
I've found that if your database name contains a dash, that it will break sp_MSforeachdb unless you don't encapsulate your code with []:

EXEC sp_MSforeachdb 'IF ''[?]'' NOT IN (''tempDB'',''model'',''msdb'')


FROM [?].sys.database_files

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 16 Sep 2012
Article Copyright 2012 by aasim abdullah
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid