Tuesday, 12 February 2019

SQL Server Row count on all the tables

This SQL script is used to get the number of records in each tables

SELECT o.NAME, 
       i.rowcnt 
FROM   sysindexes AS i 
       INNER JOIN sysobjects AS o 
               ON i.id = o.id 
WHERE  i.indid < 2 
       AND Objectproperty(o.id, 'IsMSShipped') = 0 
ORDER  BY o.NAME 


Alternative ways

SELECT o.NAME, 
       ddps.row_count 
FROM   sys.indexes AS i 
       INNER JOIN sys.objects AS o 
               ON i.object_id = o.object_id 
       INNER JOIN sys.dm_db_partition_stats AS ddps 
               ON i.object_id = ddps.object_id 
                  AND i.index_id = ddps.index_id 
WHERE  i.index_id < 2 
       AND o.is_ms_shipped = 0 
ORDER  BY o.NAME