Last active
September 30, 2015 00:47
-
-
Save andywhite37/1695380 to your computer and use it in GitHub Desktop.
SQL Server Count Records
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 @TableName nvarchar(50); | |
declare @RecordCount int; | |
declare @Sql nvarchar(255); | |
declare TableCursor cursor for | |
select t.table_name from information_schema.[tables] t | |
order by t.table_name; | |
open TableCursor; | |
fetch next from TableCursor into @TableName; | |
while @@fetch_status = 0 | |
begin | |
set @Sql = N'select @RecordCountOut = count(*) from [' + @TableName + '];' | |
exec sp_executesql @Sql, N'@RecordCountOut int output', @RecordCountOut = @RecordCount output; | |
print @TableName + ' - ' + convert(nvarchar, @RecordCount); | |
fetch next from TableCursor into @TableName; | |
end; | |
close TableCursor; | |
deallocate TableCursor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment