Update Statistics on schema level

  • Hello folks,

    I'm currently looking for a script that would accept a schema name as a parameter and would update statistics of all tables belonging to that schema.

    Anything available?

    Thanks a lot in advance!

    JM

  • You can use this script to get all the objects in a specified schema and loop thru to update the statistics.

    select * from sys.objects o join sys.schemas s

    on o.schema_id=s.schema_id

    join sys.indexes i

    on o.object_id = i.object_id

    and is_ms_shipped = 0

    On side note, you may want to catch up on STATISTICS here.

    All about SQL Server Statistics

    [font="Courier New"]Sankar Reddy | http://SankarReddy.com/[/url][/font]

  • Declare @sql Nvarchar(MAX)

    Set @sql = N''

    Select @sql = @sql + N'

    EXEC UPDATE STATISTICS ''['+s.[name]+N'].['+o.[name]+N']'';'

    From sys.objects o join sys.schemas s

    on o.schema_id=s.schema_id

    join sys.indexes i

    on o.object_id = i.object_id

    and is_ms_shipped = 0

    Print N'Executing:

    '+@sql+'

    '

    Exec(@SQL).

    Change the query to add filters according to your needs.

    "Keep Trying"

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply