鉴于TSQL
中的方案,我想从数据库中返回各个表中存在的记录数
示例:架构= ABC,表= ABC.T1,ABC.T2,ABC.T3
查询的输出:
No of records Table
10 T1
5 T2
36 T3
答案 0 :(得分:2)
如果不需要事务一致的行数,则可以查询DMV。对于大型表,这将比SELECT COUNT(*)
快得多:
SELECT t.name, SUM(p.rows) AS row_count
FROM sys.tables AS t
JOIN sys.partitions AS p ON p.object_id = t.object_id AND p.index_id IN(0,1)
WHERE OBJECT_SCHEMA_NAME(t.object_id) = N'ABC'
GROUP BY t.name;
答案 1 :(得分:1)
使用union all
:
select count(*) as [# of records], 't1' as [Table]
from abc.t1
union all
select count(*), 't2'
from abc.t2
union all
select count(*), 't3'
from abc.t3;