Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Sp MSForEachtable

From Wiki

Jump to: navigation, search

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

  1. EXEC sp_MSForEachtable 'EXEC sp_spaceused ''?'''

The following code will return a rowcount for every table

  1. EXEC sp_MSForEachtable 'select ''?'',count(*) from ?'

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.

  1. CREATE TABLE #temp (TableName VARCHAR(200),TableCount BIGINT)
  2. INSERT #temp
  3. EXEC sp_MSForEachtable 'select ''?'',count(*) from ?'
  4.  
  5. SELECT * FROM #temp


Contributed by: --SQLDenis 15:38, 31 May 2008 (GMT)


Part of SQL Server Programming Hacks

Section Undocumented but handy

281 Rating: 2.5/5 (56 votes cast)