I have written following script every time when I am asked by our team leaders or managers that how many rows are there in any particular table or sometime I am even asked which table has highest number of rows. Being Sr. Project Manager, sometime I just write down following script myself rather than asking my developers.
This script will gives row number for every table in database.
USE AdventureWorks
GO
SELECT OBJECT_NAME(OBJECT_ID) TableName, st.row_count
FROM sys.dm_db_partition_stats st
WHERE index_id < 2
ORDER BY st.row_count DESC
GO

Reference : Pinal Dave (http://blog.SQLAuthority.com)




Nice trick there. I’m saving this one for later uses. Thanks!
Hi Pinal,
I wanted to know that is there any query to delete a particular column value from a table.
Or any other alternative for the same.
Suppose I have a row as
Id Name Grade
1 Guru A
Now if I only want to delete Grade-’A',
Can I do that?
How do you do the same in SQL 2000 ?
Great solution!!
Hello Pinal!
i was talking to my brother about this post, and as other readers we proved this script not to work on SQL Server 2000, because this older DBMS does not have the object sys.dm_db_partition_stats :(
Fortunally we came up with this little alternative scrit that works (we think) perfectly on both DBMS.
Tell us what you think about it, and what are the pros and cons vs the one you posted.
Thanks in advance ;)
SELECT OBJECT_NAME(id),rowcnt
FROM SYSINDEXES
WHERE OBJECTPROPERTY(id,’isUserTable’)=1 AND indid < 2
ORDER BY rowcnt DESC
Best Regards,
Afonso.
One can count the numbers of rows in a table by using
count(@@rowcount) as mentioned here–
select count(@@rowcount) from TableName
Thanks! very helpful.
Hi,
How can I find the total number of rows in a specific table without using query like SELECT COUNT(*) FROM TableName
Hi,
How can I retrieve the rows of a table where any column of the row contains NULL, while I have no knowledge about the any column name of the table
@Neeraj. (First Post)
Do you mind reading the post, for which you have written a comment.
Please read Pinal Dave post on this web page.
~IM.
Great code, simple and useful
count is mismatching in some tables(large size db)