May 30, 2006 at 5:42 am
Hi,
Can any one help to find the no of rows in every table of a Database using system table?
May 30, 2006 at 8:02 am
this will work but might take a while to run on a many tabled DB with multi million row tables. Count_Big is, i think, only available in sql2005 if not on this version replace count_big with count.
create table DBA_RowNumber
(id int identity, table_name varchar (1000), No_Of_Rows bigint)
go
declare @no int, @i int, @sql nvarchar(4000)
declare @tab table (id int identity, table_name varchar (1000), column_name varchar(1000))
insert @tab
select distinct o.name , c.COLUMN_NAME
from sysobjects o
join information_schema.columns c
on c.table_name = o.name
and ORDINAL_POSITION=1
where o.type = 'u' --user tables only
select @no =scope_identity() , @i = 1 ,@sql =''
while @i <= @no
begin
select @sql = 'insert DBA_RowNumber (table_name,No_Of_Rows)'+char(10)
+' select ''' +table_name+''', count_big('+column_name+')from ' +table_name
from @tab
where id = @i
print @sql
exec sp_executesql @sql
set @i= @i +1
end
select * from DBA_RowNumber
May 30, 2006 at 8:56 am
Or:
o.name, i.rowcnt
o.xtype = 'U'
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 30, 2006 at 9:05 am
May 30, 2006 at 9:06 am
yes they have a spurious entry with indid = 0. indid 1 is always clustered.
I don't know what the forward-compatible, MS-sanctioned version of the code is though.
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 30, 2006 at 9:20 am
May 30, 2006 at 9:27 am
yes the information schema views as well as having an irritatingly long schema name are still quite basic and designed more like reports than useful programming tools. the sys schema has lots of good stuff in it though. But I don't know if this value is still available anywhere in the new views.
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 31, 2006 at 4:46 am
Thanks all for responding to my query...
gr8 stuff....
May 31, 2006 at 5:19 am
Note that you should run "DBCC UPDATEUSAGE (MyDatabase)" first for Tim's query to be accurate. That is the best method though.
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
May 31, 2006 at 6:46 am
True for v8 and earlier. In v9, MS have actually managed to keep the row count (and the good old rowmodctr) updated automatically. How clever!
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 31, 2006 at 7:01 am
May 31, 2006 at 7:23 am
sys.partitions.rows (bigint) - Approximate number of rows in this partition.
Note approx, but then given we are finding the height of all tables in the DB, this sounds like more of an admin task (perhaps planning or maintenance) than say a stats report. So approx. counts would probably do.
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 31, 2006 at 7:29 am
I didn't know that (about the v9 update) - thanks Tim. The boys at MS have been working hard...
Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
May 31, 2006 at 7:30 am
June 1, 2006 at 1:21 am
Still, it's essential to first make the decision if the count needed should be approximate or absolute.
For all purposes, if approximate is good enough, then any of the variants looking at the metadata in the systables is ok.
If you do need an absolute and correct value, no deviations allowed, then there's no substitute for the good old 'select count(*) from myTable' - not even in 2005. It's been enhanced in 2005, yes, but still doesn't guarantee realtime correctness.
/Kenneth
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply