獲得錶行計數

如果 table_name 被你要查詢的表替換,則可以使用以下示例查詢資料庫中特定表的總行數:

SELECT COUNT(*) AS [TotalRowCount] FROM table_name;

通過使用以下指令碼基於表的 HEAP(index_id = 0)或叢集聚簇索引(index_id = 1)連線回表的分割槽,還可以獲取所有表的行計數:

SELECT  [Tables].name                AS [TableName],
        SUM( [Partitions].[rows] )    AS [TotalRowCount]
FROM    sys.tables AS [Tables]
JOIN    sys.partitions AS [Partitions]
    ON  [Tables].[object_id]    =    [Partitions].[object_id]
    AND [Partitions].index_id IN ( 0, 1 )
--WHERE    [Tables].name = N'table name' /* uncomment to look for a specific table */
GROUP BY    [Tables].name;

這是可能的,因為每個表本質上都是一個分割槽表,除非向其新增額外的分割槽。該指令碼還具有不干擾對錶行的讀/寫操作的好處。