May 5, 2009 at 8:50 am
MS SQL Server 2000 -
I ran this to list all tables and their row counts for a Database.
select distinct convert(varchar(30),object_name(a.id)) [Table Name], a.rows from sysindexes a inner join sysobjects b on a.id = b.id
Can anyone tell me please how to modify this so that it also returns the DB owner for each table?
May 5, 2009 at 9:04 am
Try this. I think this will work, but I do not have a 2000 instance to test with.
select
a.rows , c.*
from sysindexes a
inner join INFORMATION_SCHEMA.TABLES C
on object_name(a.id) = C.TABLE_NAME
May 5, 2009 at 9:09 am
Thanks... I have about 60 rows in this one table and your select returned over 400 rows.. duplicated most of the tables many times.. I'll keep playing around with it..
May 5, 2009 at 9:39 am
Ok. I played around with it a little more. Try this one.
Select DISTINCT b.rowcnt, A.*
FROM INFORMATION_SCHEMA.TABLES A
JOIN sysindexes B on OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME) = B.id
May 5, 2009 at 9:43 am
Well, this might give you a little more information than what you asked for. Not only does it include the owner name, but also the table name, as well as the # of rows in each table.
if OBJECT_ID('tempdb..#DBCounts') is not null DROP TABLE #DBCounts
create table #DBCounts (TableName sysname, RowQuantity int)
execute sp_msforeachtable 'insert into #DBCounts select ''?'', count(*) from ?'
select * from #DBCounts
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 5, 2009 at 11:48 am
this one runs quicker for me...displays only user tables
SELECT
a.name, object_name (i.id) TableName, rows as RowCnt
FROM sysindexes i INNER JOIN sysobjects o ON (o.id = i.id AND o.xType = 'U')
inner join sys.sysusers a on o.uid = a.uid
WHERE indid < 2
ORDER BY 3 desc, TableName
May 5, 2009 at 11:57 am
Thank you all so much for your help!!! The last one worked best for me!
May 5, 2009 at 11:19 pm
Just make sure you have updates statistics before you query for RowCnt from sysindexes.
It does not reflect real number of records all the time.
_____________
Code for TallyGenerator
May 6, 2009 at 6:23 am
Sergiy (5/5/2009)
Just make sure you have updates statistics before you query for RowCnt from sysindexes.It does not reflect real number of records all the time.
Yes with
DBCC UPDATEUSAGE ('database_name','table_name') WITH COUNT_ROWS.
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