Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
HI,I need a query to get the list of tables and the count of the rows in the table in a particular database, Can any one help me in this.
vaibhavktiwari83
Aged Yak Warrior
843 Posts
Posted - 2010-05-10 : 05:52:00
Use this -create table #rowcount (tablename varchar(128), rowcnt int)exec sp_MSforeachtable 'insert into #rowcount select ''?'', count(*) from ?'select * from #rowcountorder by tablenamedrop table #rowcountVaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER
lionofdezert
Aged Yak Warrior
885 Posts
Posted - 2010-05-10 : 06:08:26
select distinctsys.tables.name,sys.partitions.rowsfrom sys.partitions inner join sys.tables on sys.partitions.object_id = sys.tables.object_idORDER BY SYS.partitions.rows desc
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts
Posted - 2010-05-10 : 06:34:10
Not distinct, that'll give some odd results if tables are partitioned. Also no need for distinct if you limit the index id or 0 or 1
SELECT OBJECT_NAME(p.object_id) AS TableName, SUM(p.rows) AS TotalRowsFROM sys.partitions pWHERE index_id IN (0,1) -- just the cluster/heap. Nonclustered indexes would add dup rowsGROUP BY object_id