auto create statistic script

--生成用户创建的statistic脚本
select
object_name(stout.object_id),
stout.name,
'CREATE STATISTICS '+ stout.name+' ON '+ Schema_name(tab.schema_id)+'.'+object_name(stout.object_id) +'(' +
STUFF(( select ',' + col.name
from sys.stats stin
join sys.stats_columns stc on stin.object_id = stc.object_id and stin.stats_id = stc.stats_id
join sys.columns col on stc.column_id = col.column_id and stc.object_id = col.object_id
where stout.object_id = stin.object_id and stout.stats_id = stin.stats_id
FOR xml path('')), 1, 1, '') +')'+case when stout.filter_definition is not null then ' WHERE '+stout.filter_definition else '' end + ';' as script
from sys.stats stout
join sys.tables tab on stout.object_id = tab.object_id
where
stout.[object_id] = object_id( 'BigTable')
and stout.name not like 'WASys_%'--filter out system created
and user_created>0

你可能感兴趣的:(script)