Sometimes as DBA’s we need to perform certain action on all the tables in a database. Say, running DBCC Checktable, or disabling all constraints before a bulk load or simply finding the size of each table. There are many ways to do it but one quick way could be using the undocumented stored procedure sp_msforeachtable which can be found in the MASTER database.
For example; running sp_spaceused on all the tables in your database:
USE AdventureWorks EXECUTE sp_MSforeachtable 'sp_spaceused [?];'
You can see that sp_spaceused stored procedure (which does not require any explanation) runs on all the tables.
This undocumented stored procedure can be used in many interesting ways and in my future posts, I shall show a few things.