Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Finding no. of Rows in all tables of a datbase in one line of script Expand / Collapse
Author
Message
Posted Monday, January 10, 2011 8:56 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 15, 2014 10:38 AM
Points: 14, Visits: 45
Comments posted to this topic are about the item Finding no. of Rows in all tables of a datbase in one line of script


Post #1045675
Posted Tuesday, January 11, 2011 3:58 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 15, 2014 2:26 AM
Points: 862, Visits: 351
I wouldn't recommend running this on a very large database, you'd be waiting for some time and would cause a lot of contention, and to be honest I wouldn't use sp_MSforeachtable unless absolutely necessary...
Post #1045776
Posted Tuesday, January 11, 2011 4:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 3:45 PM
Points: 4, Visits: 72
i've done a T-SQL script to get the same result, but returns a layout that's easier to analyze the results


set nocount on
go
create table #Tables_X_Record_Count
(table_name varchar(max),
record_count integer)
go
declare
@cursor CURSOR,
@table_name varchar(max),
@record_count integer


set @cursor = cursor for
select name from
(
select * from sysobjects where xtype = 'U'
) user_tables order by name
open @cursor

fetch next from @cursor into @table_name
while (@@fetch_status<>-1)
begin
INSERT INTO #Tables_X_Record_Count (table_name) VALUES (@table_name)

exec (' update #Tables_X_Record_Count set record_count = (select COUNT(*) as contagem FROM '+@table_name+') where table_name = '''+@table_name+'''')

fetch next from @cursor into @table_name
end

close @cursor
deallocate @cursor

select * from #Tables_X_Record_Count
drop table #Tables_X_Record_Count
set nocount off
Post #1045789
Posted Tuesday, January 11, 2011 6:17 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 10:11 AM
Points: 6, Visits: 201
I prefer using sp_spaceused to find table counts quickly. If you use OBJECT_DEFINITION() to find the body of sp_spaceused, you will see how they are determining the table count. Tying that up to all user tables, I get a fairly simple query below for my results:

select object_name(p.object_id),
SUM(case when (index_id < 2) then row_count else 0 end)
from sys.dm_db_partition_stats p inner join sys.tables t on p.object_id = t.object_id
where t.type = 'U'
group by p.object_id
order by object_name(p.object_id)
Post #1045848
Posted Tuesday, January 11, 2011 6:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 11, 2014 7:00 AM
Points: 14, Visits: 600
cvcscvcs: Use of CURSOR is avoidable here (and almost everywhere else) when you bang your head against the wall long enough.

Same output format as yours:

SELECT name, sum(row_count) AS MAX_MSG_ID
FROM sys.dm_db_partition_stats WITH (NOLOCK), sysobjects WITH (NOLOCK)
WHERE xtype = 'U'
AND object_id = object_id('' + name + '')
GROUP BY name

Post #1045853
Posted Tuesday, January 11, 2011 6:49 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 15, 2014 2:26 AM
Points: 862, Visits: 351
I agree with the last two postings, if you're confident your database stats are correct always try and use the Dynamic Management Views to looks at the table details if you're looking on SQL 2005 and above, and if you're using SQL 2000 I'd recommend using sp_spaceused as using 'SELECT COUNT(*)' can cause too many contention issues (and even when doing a count on one table try and do it for a single column, such as SELECT COUNT(ID))...
Post #1045861
Posted Tuesday, January 11, 2011 9:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 17, 2014 8:55 AM
Points: 4, Visits: 28
As "SELECT COUNT(*) FROM..." will be slow for large tables and may lock/block tables I suggest run the following command instead:

sp_MSforeachtable @command1="print '?'", @command2="sp_spaceused '?'"

To see the pretty big difference between the two please run each of the following statements against a 1million+ rows table (mine has over 100million rows and is no time for sp_spaceused VS 47 seconds for count):

select count(*) from clients;
exec sp_spaceused clients;
Post #1045964
Posted Tuesday, January 11, 2011 9:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 3:45 PM
Points: 4, Visits: 72
ron.mcdowell

Right! Definitely is better not to use the cursor, it works good for me
because my database was very small, but I'll never use it in this kind of
situation anymore, with the sys.dm_db_partition_stats is really much faster.
However, it has to be like Forum Newbie done on his example.
On your example, the record count is resulted with a wrong value.
Post #1045982
Posted Tuesday, January 11, 2011 9:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 11, 2014 7:00 AM
Points: 14, Visits: 600
That is what kchant means when he says
...if you're confident your database stats are correct...


If stats aren't updated, sys partitions won't be accurate.
Post #1045987
Posted Tuesday, January 11, 2011 10:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 17, 2014 8:55 AM
Points: 4, Visits: 28
It is very true that the numbers may not 100% accurate however it depends very much where you need to use the counts from all tables in a database and for what purpose. Microsoft recommends to run DBCC UPDATEUSAGE for a Databases upgraded from SQL Server 2000 that may contain invalid counts but after that they should be pretty much accurate - see "best practice" under http://msdn.microsoft.com/en-us/library/ms188414.aspx.

Besides that I believe that when you run such a query against a live database you do not expect 100% accurate counts because if you think: by the time ALL the counts are completed the numbers in some of the tables will be off due to DELETEs and INSERTs that are inevitable in a database. Hope this clarifies my suggestion to use sp_spaceused.
Post #1046005
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse