Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
sp_MSForEachtable is an undocumented procedure so you should use it with caution. It is very handy if you need to do something to all the tables or know something about all the tables
For example the following code will run the sp_spaceused proc for every table
The following code will return a rowcount for every table
There is one problem with this, you get a resultset for every table. To get one resultset dump the output in a temp table first.
- CREATE TABLE #temp (TableName VARCHAR(200),TableCount BIGINT)
- INSERT #temp
- EXEC sp_MSForEachtable 'select ''?'',count(*) from ?'
- SELECT * FROM #temp
Contributed by: --SQLDenis 15:38, 31 May 2008 (GMT)
Part of SQL Server Programming Hacks
Section Undocumented but handy