Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2000: how do i get a list of tables and the row counts? [duplicate]

I know that I can get a list of tables with

SELECT TABLE_NAME FROM information_schema.tables 
WHERE NOT TABLE_NAME='sysdiagrams' 
  AND TABLE_SCHEMA = 'dbo' 
  AND TABLE_TYPE= 'BASE TABLE'

But I'm not sure how to modify that to get a 2nd column with the current count of rows for the tables. I though of something like this:

DECLARE @tbl VARCHAR(200)
(SELECT @tbl = TABLE_NAME, TABLE_NAME,
(SELECT COUNT(ID) AS Cnt FROM @tbl)
FROM information_schema.tables 
WHERE NOT TABLE_NAME='sysdiagrams' 
  AND TABLE_SCHEMA = 'dbo' 
  AND TABLE_TYPE= 'BASE TABLE')

I know the above is not valid T-SQL but I think it gets the point of what I would like the have done. This is for SQL Server 2000. I would prefer not to use store procedures if at all possible.

like image 854
Justin808 Avatar asked Mar 03 '11 21:03

Justin808


1 Answers

A quick and dirty way (includes uncommitted changes and possibly forwarding pointers on heaps)

select o.name, rows 
from sysindexes i join sysobjects o on o.id=i.id
where indid < 2 and type='U'
like image 84
Martin Smith Avatar answered Sep 20 '22 14:09

Martin Smith