Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sarangbk/01549440ef73e8729b19baf0608c08c8 to your computer and use it in GitHub Desktop.
Save sarangbk/01549440ef73e8729b19baf0608c08c8 to your computer and use it in GitHub Desktop.
Get RowCount of all tables in a MSSQL database
-- Run this against the database from which you want the row counts
-- Ref https://msdn.microsoft.com/en-us/library/ms190324.aspx
SELECT
QUOTENAME(SCHEMA_NAME(sysObjects.schema_id)) + '.' + QUOTENAME(sysObjects.name) AS [Table]
, SUM(sysPartitions.Rows) AS [Rows]
FROM
sys.objects AS sysObjects
INNER JOIN sys.partitions AS sysPartitions
ON sysObjects.object_id = sysPartitions.object_id
WHERE
sysObjects.type = 'U' -- U = Table (user-defined) (https://msdn.microsoft.com/en-us/library/ms190324.aspx)
AND sysObjects.is_ms_shipped = 0x0 --Object is created by an internal SQL Server component. Ignore these objects from the query
AND index_id < 2 -- We need all Heap (0) and Clustered (1) indices
GROUP BY
sysObjects.schema_id, sysObjects.name
ORDER BY [Table]
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment