Solved

sp_Msforeachtable only for system tables

Posted on 2011-02-25
4
670 Views
Last Modified: 2012-05-11
how can we make the following idea work?

sp_Msforeachtable 'select "?",count(*) from ? where "?" in (select name from sys.tables where type = ''s'''
0
Comment
Question by:anushahanna
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 150 total points
ID: 34981445
This is how you can skip some tables when using this undocumented procedure.
http://www.sqlservercurry.com/2010/06/how-to-skip-table-or-database-while.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+sqlservercurry/blog+(Sql+Server+2005/2008+Blog+With+Tips+Tricks+and+Articles)

But I don't think you can use this proc to get row count for system tables.
0
 
LVL 17

Assisted Solution

by:dbaSQL
dbaSQL earned 350 total points
ID: 34981570
Remember, sp_msforeachtable is undocumented, and unsupported.  But try this:


EXECUTE sp_MSforeachtable
@precommand = 'CREATE TABLE ##Results
( name nvarchar(128),
rows char(11)
)',
@command1 = 'SELECT ''?'' as TableName, COUNT(1) as TotalRows FROM ? WITH(NOLOCK)',
@whereand = 'or OBJECTPROPERTY(o.id, N''IsSystemTable'') = 1',
@postcommand = 'SELECT * FROM ##Results; DROP TABLE ##Results'
Go
0
 
LVL 17

Accepted Solution

by:
dbaSQL earned 350 total points
ID: 34981632
Actually, this is better:

EXECUTE sp_MSforeachtable
@precommand = 'CREATE TABLE ##Results
( name nvarchar(128),
rows char(11)
)',
@command1 = 'INSERT ##Results SELECT CAST(OBJECT_NAME(id) AS VARCHAR(12)) AS ''Table'',CAST(rowcnt as varchar(6)) AS ''#Rows'' FROM sysindexes WHERE indid IN (0,1) AND OBJECTPROPERTY(id, ''IsUserTable'') = 0 GROUP BY id, rowcnt, reserved, dpages ORDER BY ''Table'';',
@postcommand = 'SELECT * FROM ##Results; DROP TABLE ##Results'
Go
0
 
LVL 6

Author Comment

by:anushahanna
ID: 34982644
very good logic - thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now