/ 文档备忘 / 0浏览

SQL Server 计算表记录数和占用空间

USE 数据库名字
GO

SELECT     obj.name AS TableName, prt.rows AS TotalRows, SUM(alloc.used_pages) * 8 AS [SpaceUsed(KB)]
FROM         sys.objects AS obj INNER JOIN
                      sys.indexes AS idx ON obj.object_id = idx.object_id INNER JOIN
                      sys.partitions AS prt ON obj.object_id = prt.object_id INNER JOIN
                      sys.allocation_units AS alloc ON alloc.container_id = prt.partition_id
WHERE     (obj.type = 'U') AND (idx.index_id IN (0, 1))
GROUP BY obj.name, prt.rows
ORDER BY TableName