I can't tell you how many countless times I loaded up a saved T-SQL cursor script to do some sort of looping over all the user tables in a database.  Well, APPARENTLY there is an undocumented system sproc named "sp_MSforeachtable" which does exactly what it says....it will do something for each table in the current database.  I stumbled upon this over at Joe Webb's blog

Here is an example of its use.

USE AdventureWorks;
EXECUTE sp_MSforeachtable 'SELECT COUNT(*) FROM ?';

There are obviously easier ways to get table counts but I just used this as an easy example.  The "?" is a special character that gets replaced by the actual table name.  Also, the T-SQL command in quotes must be something that can be executed.  Joe uses examples such as checking the amount of space each table is using.  This can really come in handy for doing those "maintenance" tasks on all your tables in a database quickly and easily.  You could write an SSIS package or a scheduled task to perform all this maintenance nightly using the sproc above.


Posted in:   Tags:

Comments


Saudi Arabia A. Hinnawi
March 24. 2008 19:58
A. Hinnawi
nice --- very nice

no site


January 25. 2010 07:15
cash loans
We must have a theme, a goal, a purpose in our lives.

http://cashusloans.com/http://cashusloans.com/


January 28. 2010 18:09
Miami Computer IT Support
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post....

http://www.telxcomputers.com/http://www.telxcomputers.com/


January 28. 2010 18:10
Zend CMS
That article is so interesting and makes a very nice image in my mind. That is very much helpful in nominating the board of directors.

http://www.zundoo.com/http://www.zundoo.com/


January 28. 2010 18:10
Datacenter Transformation
It's very informative posting, actualy i'm new in the domain matter, so this writing help me much increase my knowledge.

http://www.hclisd.com/Datacenter-Transformation-Services.aspxhttp://www.hclisd.com/Datacenter-Transformation-Services.aspx


January 28. 2010 18:11
Dedicated Servers UK
Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post.

http://www.serverspace.co.uk/http://www.serverspace.co.uk/


January 28. 2010 18:11
Video Marketing Services
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

http://www.i-comm.tv/http://www.i-comm.tv/


January 28. 2010 18:11
IT support kent
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them. To me, you are doing the great work. Carry on this. work at home In the end, I would like to thank you for making such a nice website.

http://www.rascalcomputing.com/http://www.rascalcomputing.com/


January 31. 2010 23:32
Printer Ink
I rarely encourage the use of iteration when database programming because looping constructs in the database world tend to be harder to manage and much slower than set-based constructs. However, there are times when using iteration inside the database engine is useful. I’ll present a few examples using two undocumented system stored procedures provided by Microsoft. (Note: The examples in this article work in SQL Server 2000 and SQL Server 2005.)

http://www.professorink.com/http://www.professorink.com/


February 1. 2010 08:05
Loans in New York
The human race has one really effective weapon, and that is laughter.

http://superpaydayloan.com/state/New-Yorkhttp://superpaydayloan.com/state/New-York


February 6. 2010 00:17
dstarlight
an interesting ... Smile

still learning
Ace dstarlight

http://dstarlight.com/http://dstarlight.com/


February 8. 2010 05:25
fast cash loans
What is a friend? I will tell you... it is someone with whom you dare to be yourself.

http://cashusloans.com/http://cashusloans.com/


February 9. 2010 02:52
payday loans
Work is much more fun than fun.

http://fastloansus.com/http://fastloansus.com/


United States PNG
February 9. 2010 08:36
PNG
This can really come in handy for doing those "maintenance" tasks on all your tables in a database quickly and easily.  

http://www.777icons.com/http://www.777icons.com/

Search Blog

Blog Roll

    OPMLDownload OPML file

    Recent Comments

    Banners

    Theme Grabber
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010 Tony Testa's World