Skip to content

Instantly share code, notes, and snippets.

@NoahDragon
Created February 26, 2016 15:00
Show Gist options
  • Save NoahDragon/830322b4af06cdd2b265 to your computer and use it in GitHub Desktop.
Save NoahDragon/830322b4af06cdd2b265 to your computer and use it in GitHub Desktop.
Get Row Count for All Tables in A Database.
declare @tableName VARCHAR(256), @id INT, @sql NVARCHAR(MAX)
set @id = 0
while(1=1)
begin
SELECT Top 1
@tableName = name,
@id = object_ID
FROM sys.objects
WHERE name LIKE 'Import%' -- Change to the table you would like to get the count.
AND type = 'U'
AND object_id > @id
ORDER BY object_ID
IF @@ROWCOUNT = 0
break
SET @sql = 'SELECT ''' + @tableName + ''' AS TableName, COUNT(1) AS CountRow FROM ' + @tableName
print @sql
EXEC (@sql)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment