我們都知道用聚合函數count()可以統計表的行數。如果需要統計數據庫每個表各自的行數(DBA可能有這種需求),用count()函數就必須為每個表生成一個動態SQL語句并執行,才能得到結果。以前在互聯網上看到有一種很好的解決方法,忘記出處了,寫下來分享一下。
select schema_name(t.schema_id) as [Schema], t.name as TableName,i.rows as [RowCount]
from sys.tables as t, sysindexes as i
where t.object_id = i.id and i.indid =1