SQL count rows in a table

I need to send an SQL query to a database that will tell me how many rows are in the table. I could get all the rows in the table using SELECT and then count them, but I don't like to do it that way. Is there any other way to set the number of rows in a table on an SQL server?

+7
sql-server count rows database-table
source share
5 answers

Yes, SELECT COUNT(*) FROM TableName

+16
source share

Use this query:

 Select S.name + '.' + T.name As TableName , SUM( P.rows ) As RowCont From sys.tables As T Inner Join sys.partitions As P On ( P.OBJECT_ID = T.OBJECT_ID ) Inner Join sys.schemas As S On ( T.schema_id = S.schema_id ) Where ( T.is_ms_shipped = 0 ) AND ( P.index_id IN (1,0) ) And ( T.type = 'U' ) Group By S.name , T.name Order By SUM( P.rows ) Desc 
+2
source share
 select sum([rows]) from sys.partitions where object_id=object_id('tablename') and index_id in (0,1) 

very fast, but very rarely inaccurate.

+1
source share

Here is the request

 select count(*) from tablename or select count(rownum) from studennt 
0
source share

Why don't you just right-click on the table, and then properties → Storage, and it will tell you the number of rows. You can use below to count rows in a view

 SELECT SUM (row_count) FROM sys.dm_db_partition_stats WHERE object_id=OBJECT_ID('Transactions') AND (index_id=0 or index_id=1)` 
-one
source share

All Articles