Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sp_Msforeachtable only for system tables

Posted on 2011-02-25
4
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 600 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 1400 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 1400 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

688 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