Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select count(*) from all tables

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
Hi,

I need to be able to do a select count(*) from all the tables in a database (SQL Server D/B). Usually I would do
select count(*) from (select table_name from all_tables);
but I'm not sure how to do it in SQL Server.
I'd be much obliged for any help.

Thanks.
 
This is probably not exactly what you want, but you should be able to modify it. The key is the sp_msforeachtable stored proc.

set quoted_identifier off

sp_msforeachtable
"declare @count int select @count=count(*)from ? if @count > 0 begin print '?' print @count end"
 
Here is another method that is very fast. It uses the sysindexes table so you need to make sure statistics are up-to-date.

Select o.name, i.rows
from sysindexes i
inner join sysobjects o
on i.id=o.id
where i.indid =0
and o.type='u' Terry L. Broadbent
Programming and Computing Resources
 
Thanks very much for your help, I'll try these methods out.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top