Skip to content

Instantly share code, notes, and snippets.

@azcoov
Created March 14, 2011 15:42
Show Gist options
  • Save azcoov/869338 to your computer and use it in GitHub Desktop.
Save azcoov/869338 to your computer and use it in GitHub Desktop.
Update Statistics for all tables
create sp_UpdateStatistics
as
/*
This procedure will run UPDATE STATISTICS against
all user-defined tables within this database.
*/
declare
@tablename varchar(255),
@tablename_header varchar(255)
declare tnames_cursor cursor for
select
'[' + ss.name + '].[' + so.name + ']'
from
sys.objects so
join sys.schemas ss on ss.schema_id = so.schema_id
where
type = 'u'
order
by ss.name, so.name
open tnames_cursor
fetch next from tnames_cursor into @tablename
while (@@fetch_status <> -1)
begin
if (@@fetch_status <> -2)
begin
select @tablename_header = 'Updating ' + rtrim(upper(@tablename))
print @tablename_header
exec ('UPDATE STATISTICS ' + @tablename )
end
fetch next from tnames_cursor into @tablename
end
select @tablename_header = '****** NO MORE TABLES ******'
print @tablename_header
print 'Statistics have been updated for all tables.'
deallocate tnames_cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment