Solved

sp_Msforeachtable only for system tables

Posted on 2011-02-25
4
668 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

910 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

17 Experts available now in Live!

Get 1:1 Help Now