Created
March 14, 2011 15:42
-
-
Save azcoov/869338 to your computer and use it in GitHub Desktop.
Update Statistics for all tables
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
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