December 11, 2008 at 3:16 am
How can i get the Number of Records in each table in a database.
Desired output as below
Table Name RecordCount
Table1 10
Table2 20
Table3 5
Table5 100
December 11, 2008 at 3:29 am
create table #rowcount (tablename varchar(128), rowcnt int)
exec sp_MSforeachtable
'insert into #rowcount select ''?'', count(*) from ?'
select * from #rowcount
order by tablename
drop table #rowcount
December 11, 2008 at 3:36 am
One way u can use sysindexes table to get the row count of all the table on that databases 🙂
December 11, 2008 at 3:39 am
CrazyMan (12/11/2008)
One way u can use sysindexes table to get the row count of all the table on that databases 🙂
This not always accurate, all this has been discussed in depth here
http://www.sqlservercentral.com/Forums/Topic604340-149-1.aspx
December 11, 2008 at 3:47 am
select object_name(id), rowcnt from sysindexes where id in (select id from sysobjects where type = 'u') and indid = 0
Use the above code.
December 11, 2008 at 3:59 am
Hi Steve
I do agree, i forget to mention that :), depends upon the usage
December 11, 2008 at 4:09 am
This not always accurate, all this has been discussed in depth here
http://www.sqlservercentral.com/Forums/Topic604340-149-1.aspx
Yes exactly... the sysindexes might not have correct values in all the cases.
I think you better use the code
create table #rowcount (tablename varchar(128), rowcnt int)
exec sp_MSforeachtable
'insert into #rowcount select ''?'', count(*) from ?'
select * from #rowcount
order by tablename
drop table #rowcount
December 11, 2008 at 4:28 am
course - if you have any big tables that would take a while.
We use sysindexes - as count(*) over 2 billion records may offend.
Oh and on 2000 at least sp_spaceused seems to break once you hit the int limit on rows - but there is a second column in sysindexes that handles it 🙂
December 11, 2008 at 7:20 am
Failing to plan is Planning to fail
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply