Last active
July 24, 2018 19:45
-
-
Save Hendy/6f2813c40a5d3bffa1096490ac38496c to your computer and use it in GitHub Desktop.
MS SQL Get the row counts of all tables in current database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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