SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Confio Ignite 8

Use sp_MSForEachDB Instead of Your Own Loop

Did you know it's possible to loop through databases and tables without writing your own loop? That's right, SQL Server has a couple of stored procedures that handle the looping for you, all you provide is the work!

Duration:
2 mins 0 secs
Skill Level:
100
Rating:
4.23 out of 5
Publish Date:
October 02, 2009
Use sp_MSForEachDB Instead of Your Own Loop You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Andy Warren
Andy Warren is a software trainer focusing on SQL Server, a member of the PASS Board of Directors, and a principal in this site - SQLShare.com.

References

There are no downloads or recommended reading links for this video

Comments
Talib on 10/7/2009
not well explained ; only those who have good understanding can understand the purpose of that un-documented procedure

Christian Bahnsen on 10/7/2009
Thanks. Ya learn something new everyday.

kafe on 10/7/2009
I wrote an sp like a year ago to do so... jajajaja

Robert McCormick on 10/7/2009
Great. I knew about sp_foreachtable, but not about sp_foreachdb

Tonci on 10/7/2009
Good and to the point! Thank you, Tonci.

Feodor Georgiev on 10/7/2009
It is possible to limit the ms_foreachdb to certain databases. I have done it before.

Pehle on 10/7/2009
Excellent video...

Darren Bates on 10/7/2009
@Feodor: do you have a link? and does the method also work for sp_msforeachtable?

Neal Pawlowski on 10/7/2009
Aren't those undocumented procs meaning - no MS support if something "blew up" as a result of using them?

Rod on 10/8/2009
I never heard of sp_MSForEach system SP's before. This is great information. Thanks!

Feodor Georgiev on 10/8/2009
@Darren Bates: Hello Darren, I am planning on filming a video myself tomorrow about this. It turned out that there are loads of things you can actually control. A while back I had used sp_MSForEachDB to run a procedure towards all user dbs. And today I went exploring the possibilities even further, and I think they are endless.

Feodor Georgiev on 10/8/2009
@Neal Pawlowski: Neal, this is true. However, sp_who2 is also undocumented and I don't see myself surviving as a DBA without it. On the other hand, when I have to do more advanced routines, I use BI tools. There are great features which help with iteration, filtering, variable mapping, etc.

Robert McCormick on 10/8/2009
From looking at the proc sp_MSforeachDB it looks like you have the option of changing the placeholder character from '?' to something else. You can also add in a pre command, a post command that are run 'as is' (no placeholder substitution), and it looks like two additional commands that allow for placeholder substitution.

Robert McCormick on 10/8/2009
[sp_MSforeachtable] allows for a little more flexibility too: "This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set @precommand and @postcommand may be used to force a single result set via a temp table.

Adam Gojdas on 10/8/2009
Be a bit cautious with these. They use a global cursor which may unintentionally cause issues depending on how you go about using these.

Steve Culshaw on 10/9/2009
Excellent tip

7CF6435DEF on 10/9/2009
It is very clear! Do not re-invent the wheel. Thank you very much

Jonathan Winer on 10/9/2009
Well communicated concepts.

Team FAB on 10/21/2009
This video doesn't play for me - I click on it and nothing happens. I've tried re-loading the page?

Team FAB on 10/21/2009
This was because the popup window was already open and hidden... sorted now.

Kamen Angelov on 10/24/2009
Very usefull info!

Rubens on 10/24/2009
Awesome explanation, really appreciated this video.

Deb Shutts on 11/10/2009
Thank you, I learn a lot from these videos!

Feodor Georgiev on 11/17/2009
I just uploaded a video which shows how to run the procedure for certain databases only. You may view it when it is featured on the site.

ML Stephens on 11/27/2009
did not know about this one thanks---can use it right away----doing conversion from 2000 to 2005

Feodor Georgiev on 12/1/2009
Here is the video which shows how to use sp_MSForEachDB with a filter. http://www.sqlshare.com/Media.aspx?vid=681

alfred on 1/27/2010
good

yeskay on 2/4/2010
Very useful... didn't konw the '?' trick..

harishkumar127 on 3/30/2010
Thank you

BAEE210825 on 8/17/2010
Issue with volume. Generally all your videos have problem with audio...

kalapala baburao on 10/8/2010
ok

anand on 10/8/2010
Informative viedo

FD928096BE on 10/17/2010
Tx.

Roxanne Lenahan on 10/29/2010
Sound was very quiet...and I had it cranked all the way up on the display, and on my laptop.

FELIX on 11/2/2010
Good video

MohanD on 11/4/2010
Good one..

mark mcnary on 11/16/2010
I am new to SQL server, and these undocumented SP's are fascinating to me

Michael on 11/16/2010
Thanks

B58E141AB8 on 12/8/2010
Good info

Russell Tye on 12/12/2010
Good information...

manjeet on 12/13/2010
Good Video

Charlie Arehart on 1/20/2011
Very nice.

chris on 6/27/2011
Sweet, looks very usefull.

Zeshan on 6/29/2011
Very Good

Jamshid Nouri on 2/20/2012
excellent demo!

Steve Culshaw on 2/20/2012
Good tip on in-built functionality

The xp_msforeachdb does not always work under heavy load, so don't rely on it for production environments.. Look at http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/

Sherwood Munk on 2/20/2012
Quick and to the point. However, this particular process would seem to have limited application.

Kristeen Sellers on 2/20/2012
Would have been nice to know before I wrote my daily DB checking scripts, but was easy to modify and implement.

Andrew on 2/20/2012
Nice to know that there are these kinds of things in SQL server.

Russell Tye on 2/20/2012
Absolutely great presentation!!!

Paul Jemiolo on 2/21/2012
I believe each of these SP's are going to be degraded soon. Still, it's a good article.

Cheryl on 2/22/2012
I'd like to see additional ways to use them.

John O'Sullivan on 2/22/2012
very useful as those sp's are not common

Jeremiah on 2/23/2012
Very useful for me. And Timely as well!!!

manjeet on 2/23/2012
good one

Carlos B. Vasquez on 3/2/2012
very useful Stored Procedures.

Kaushik on 3/19/2012
Excellent!

Munna Bhai on 4/14/2012
Excellent video!! keep up the good work.

om on 8/5/2012
its grate video



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions