Skip to content

Instantly share code, notes, and snippets.

@andywhite37
Last active September 30, 2015 00:47
Show Gist options
  • Save andywhite37/1695380 to your computer and use it in GitHub Desktop.
Save andywhite37/1695380 to your computer and use it in GitHub Desktop.
SQL Server Count Records
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