Guest User

Untitled

a guest
Oct 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. declare @indexQuery nvarchar(1000)
  2.  
  3. select @indexQuery = 'USE [MyDB]; SELECT i.index_id, i.name, s.avg_fragmentation_in_percent
  4. FROM sys.dm_db_index_physical_stats (
  5. DB_ID(),
  6. OBJECT_ID(N''MyTable''),
  7. DEFAULT, DEFAULT, DEFAULT) s, sys.indexes i
  8. WHERE s.object_id = i.object_id
  9. AND s.index_id = i.index_id;'
  10.  
  11. exec msdb.dbo.sp_send_dbmail @profile_name='Local Mail Profile', @recipients='someone@somewhere.co.uk', @subject='Re-index Started', @query = @indexQuery, @attach_query_result_as_file=1, @query_attachment_filename = 'Reindex BEFORE.txt'
  12.  
  13. select @indexQuery = 'indexoptimize ''MyDB'''
Add Comment
Please, Sign In to add comment