Created
May 13, 2020 15:31
-
-
Save lafleurh/f93cca8d0d1d3322d1334f916e286ef9 to your computer and use it in GitHub Desktop.
SQL Server get all table names and row counts
This file contains hidden or 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 NVARCHAR(MAX) | |
DECLARE @RowCounts AS TABLE (name NVARCHAR(MAX), RowCnt int) | |
DECLARE csrData CURSOR FAST_FORWARD FOR | |
SELECT | |
'SELECT ''[' + schema_name(schema_id) + '].[' + name + ']'', COUNT(*) AS RowCnt FROM [' + schema_name(schema_id) + '].[' + + name + '] (NOLOCK)' | |
FROM sys.tables | |
OPEN csrData | |
FETCH csrData INTO @sql | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
PRINT @SQL | |
INSERT INTO @RowCounts | |
EXEC (@SQL) | |
FETCH csrData INTO @sql | |
END | |
CLOSE csrData | |
DEALLOCATE csrData | |
SELECT * FROM @RowCounts ORDER BY RowCnt DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment