表中的SQL计数行

时间:2022-05-13 22:23:09

I need to send a SQL query to a database that tells me how many rows there are in a table. I could get all the rows in the table with a SELECT and then count them, but I don't like to do it this way. Is there some other way to ask the number of the rows in a table to the SQL server?

我需要向数据库发送一个SQL查询,该数据库告诉我表中有多少行。我可以使用SELECT来获取表中的所有行,然后对它们进行计数,但是我不喜欢这样做。是否有其他方法向SQL服务器询问表中的行数?

5 个解决方案

#1


22  

Yes, SELECT COUNT(*) FROM TableName

是的,从TableName中选择COUNT(*)

#2


2  

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

#3


1  

select sum([rows])
from sys.partitions
where object_id=object_id('tablename')
 and index_id in (0,1)

is very fast but very rarely inaccurate.

是非常快,但很少是不准确的。

#4


0  

Here is the Query

这是查询

select count(*) from tablename

 or 

select count(rownum) from studennt

#5


-1  

Why don't you just right click on the table and then properties -> Storage and it would tell you the row count. You can use the below for row count 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)`

#1


22  

Yes, SELECT COUNT(*) FROM TableName

是的,从TableName中选择COUNT(*)

#2


2  

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

#3


1  

select sum([rows])
from sys.partitions
where object_id=object_id('tablename')
 and index_id in (0,1)

is very fast but very rarely inaccurate.

是非常快,但很少是不准确的。

#4


0  

Here is the Query

这是查询

select count(*) from tablename

 or 

select count(rownum) from studennt

#5


-1  

Why don't you just right click on the table and then properties -> Storage and it would tell you the row count. You can use the below for row count 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)`