Friday 25 January 2013

Sql Server Table Row Count

SELECT
 s.Name as SchemaName
 ,t.NAME AS TableName
 ,sum(p.rows) as RowCounts
    FROM sys.tables t INNER JOIN sys.indexes i
   ON t.OBJECT_ID = i.object_id
   INNER JOIN sys.partitions p
   ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
   INNER JOIN sys.schemas s
   ON s.schema_id=t.schema_id
 WHERE -- heap or clustered
   i.type IN (0, 1)
 GROUP BY s.Name, t.NAME, i.object_id, i.index_id, i.name
 -- use this to check list empty table ONLY
 --HAVING SUM(p.rows) = 0
 order by 1,2

No comments:

Post a Comment