Skip to content

Instantly share code, notes, and snippets.

@Hendy
Last active July 24, 2018 19:45
Show Gist options
  • Save Hendy/6f2813c40a5d3bffa1096490ac38496c to your computer and use it in GitHub Desktop.
Save Hendy/6f2813c40a5d3bffa1096490ac38496c to your computer and use it in GitHub Desktop.
MS SQL Get the row counts of all tables in current database
DECLARE @sql VARCHAR(255)
SET @sql = 'DBCC UPDATEUSAGE ("' + DB_NAME() + '")'
EXEC(@sql)
CREATE TABLE #Tables (tableName VARCHAR(255), tableRows INT)
INSERT #Tables
EXEC sp_msForEachTable
'SELECT PARSENAME(''?'', 1),
COUNT(*) FROM ?'
SELECT
tableName AS 'Table',
tableRows AS 'Row Count'
FROM #Tables
ORDER BY tableRows DESC
SELECT SUM(tableRows) AS 'Total Row Count'
FROM #Tables
DROP TABLE #Tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment