Search This Blog

Friday, September 2, 2011

Update Statistics For Specific Schema

Hi All,

One of my fellow dba asked a question.

I have a database having multiple schema's in it. I want to update statistics for some specific schema's only?

Now how to do that. Again SP_MSFOREACHTABLE (ofcourse non documented) but the savior here.

--This will print table names first for specific schema.
Exec sp_MSforeachtable @command1 = " print '?' ", @whereand = " and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'dbo') "
--This will update statistics for specific schema.
Exec sp_MSforeachtable @command1 = " Update Statistics ? ", @whereand = " and uid = (SELECT schema_id FROM sys.schemas WHERE name = 'sales') "


So using above command we can do this. If we have multiple schema's simply change Where condition and make it WHERE NAME in ('a','b','c')



1 comment:

Anonymous said...

perfect, exactly what i was looking for! thank you for the great and useful post!