Saturday, 18 May 2013
     
AUSQLUG Forums Home   Search   Login   Register   Member List  
SQL Server 2005 Forums  > T-SQL Programming  > Rowcount from all tables in database  
 
Display using:  
Previous Thread :: Next Thread 
 Author Thread: Rowcount from all tables in database
philcart is not online. Last active: 7/01/2013 4:04:01 PM philcart
philcart.blogspot.com
Top 25 Poster
Forum Moderator
Joined: 20 Jul 2004
Total Posts: 345
 
Rowcount from all tables in database
Posted: 31 Aug 2006 07:05 PM
I can run,

[b]select rowcnt from sysindexes where indid in(0,1)[/b]

and it dutifully returns the row counts from somewhere, because sysindexes is one of those hidden backward compatibility things that you aren't meant to use.

Short of looping through a list of tables, where should I be getting the rowcounts from?


Thanks
Phill

Colt 45 - the original point and click interface ;)
[url]http://philcart.blogspot.com/[/url]
mr_fj is not online. Last active: 18/07/2007 10:05:34 AM mr_fj
Top 100 Poster
Joined: 30 Jan 2007
Total Posts: 2
 
Re: Rowcount from all tables in database
Posted: 30 Jan 2007 04:48 PM
Hi Phil,

SysIndexes works when you put an index to a table.
Also, you need to update the statistics when ever you insert data into a table to get reflected in the index table.

You can use following query to get the row-count:
SELECT
'SELECT COUNT(*) AS [RowCount] FROM ' + so.Name
FROM
SysObjects so
WHERE
so.xType = 'U'

Only issue is that you have to run this in the SQL Query Analyser to run the output.

Let me know if this solves your problem.

Cheers,
Fred
philcart is not online. Last active: 7/01/2013 4:04:01 PM philcart
philcart.blogspot.com
Top 25 Poster
Forum Moderator
Joined: 20 Jul 2004
Total Posts: 345
 
Re: Rowcount from all tables in database
Posted: 30 Jan 2007 06:36 PM
Thanks Fred

Although as my original post indicated, I was after a more supported method. Something along the lines of a DMV or INFORMATION_SCHEMA view.


Thanks
Phill

Colt 45 - the original point and click interface ;)
[url]http://philcart.blogspot.com/[/url]
urid is not online. Last active: 12/04/2010 11:08:29 AM urid
Top 25 Poster
Joined: 18 Jun 2007
Total Posts: 34
 
Re: Rowcount from all tables in database
Posted: 18 Jun 2007 05:46 PM
Hi
SELECT
t.name,
[RowCount] = SUM
(
CASE
WHEN (p.index_id < 2) AND (a.type = 1) THEN p.rows
ELSE 0
END
)
FROM
sys.tables t
INNER JOIN sys.partitions p
ON t.object_id = p.object_id
INNER JOIN sys.allocation_units a
ON p.partition_id = a.container_id
GROUP BY
t.name;

Best Regards,
Uri Dimant
SQL Server MVP
http://dimantdatabasesolutions.blogspot.com/
Greg_Linwood is not online. Last active: 20/07/2012 11:25:21 AM Greg_Linwood
www.sqlservants.com.au
Top 25 Poster
Forum Moderator
Joined: 07 Jun 2004
Total Posts: 123
 
Re: Rowcount from all tables in database
Posted: 18 Jun 2007 07:15 PM
Hi Uri

Thanks for dropping by & helping out with our forums - its appreciated very much

Regards,
Greg Linwood
urid is not online. Last active: 12/04/2010 11:08:29 AM urid
Top 25 Poster
Joined: 18 Jun 2007
Total Posts: 34
 
Re: Rowcount from all tables in database
Posted: 18 Jun 2007 08:42 PM
Hi Greg.
I hope you are doing well. No problem , I'm loving it.
Will be waiting for new artciles at your blog...

Best Regards,
Uri Dimant
SQL Server MVP
http://dimantdatabasesolutions.blogspot.com/
pleitch is not online. Last active: 14/08/2007 3:13:26 PM pleitch
Joined: 14 Aug 2007
Total Posts: 1
 
Re: Rowcount from all tables in database
Posted: 14 Aug 2007 03:12 PM
Also - Using sysindexes is only an approximate. At TechEd they outlined that under 2008 it will contain an accurate count.

Until then there is nothing more accurate than "looping through a list of tables". It can be a little faster if you use "with (nolock)" on the tables, but that will be at the price of accuracy.

Peter Brown is not online. Last active: 8/10/2007 11:03:55 AM Peter Brown
Joined: 08 Oct 2007
Total Posts: 1
 
Re: Rowcount from all tables in database
Posted: 08 Oct 2007 11:00 AM
Have a look at this page with the address below:
http://www.mountainman.com.au/software/Winluck/row_counter.html

I have been using an automated row-counter for ages.
It can be configured to alert on the condition of ROWCOUNT LOSS
is a series of designated master tables, when as a business rule,
one might never expect row-loss to occur in such tables.

Here is the code to create the stored procedure.
Simply schedule the stored procedure on a daily basis.
The rowcounter simply writes a daily tally to a history file.

Another application that can be leveraged easily from this
data is the historical growth of databases, database tables,
indexes, which is useful for planning, at times.


CREATE procedure ls_rowcounter

as

--create table rowcounts ( --

insert into rowcounts
select
getdate(),
substring(o.name,1,50),
max(i.rows),
max(refdate)

from Inpro..sysobjects o
join Inpro..sysindexes i on (o.id=i.id)
where o.type='U'

group by substring(o.name,1,50)
order by 3 desc



Best wishes,


Pete Brown
www.mountainman.com.au/software
Previous Thread :: Next Thread 
Page 1 of 1
 
AUSQLUG Forums  > SQL Server 2005 Forums  > T-SQL Programming  > Rowcount from all tables in database