有的时间我想把数据库中表的记录统计一下,如果我们一个一个表的操作可以直接select count(*) from tablename就可以然后一个个相加,但是如果有上百个表有没有更简单的方法呢,下面我总结了一些方法有需要的朋友可参考。
如果是要得到中所有表的条数呢?我们来看几种最常见的方式:
–方法一
代码如下
b.name as tablename ,
c.row_count as datacount
from sys.indexes a ,
sys.objects b ,
sys.dm_db_partition_stats c
where a.[object_id] = b.[object_id]
AND b.[object_id] = c.[object_id]
AND a.index_id = c.index_id
AND a.index_id < 2
AND b.is_ms_shipped = 0
–方法二
代码如下
select b.name as tablename ,
a.rowcnt as datacount
from sysindexes a ,
sysobjects b
where a.id = b.id
and a.indid < 2
and objectproperty(b.id, ‘IsMSShipped’) = 0
–方法三
代码如下
if exists ( select *
from dbo.sysobjects
where id = objec