Rebuild Por Base
Rebuild Por Base
GO
/****** Object: StoredProcedure [dbo].[USP_DBA_REBUILD_POR_BASE] Script Date:
08/14/2013 10:01:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
set nocount on
declare
@schema sysname ,
@tableid int ,
@tabela sysname ,
@indexid int ,
@indice sysname ,
@cmd varchar(max)
open indices
fetch next from indices into @indexid,@indice
while @@fetch_status = 0
begin
insert into ##index_stats
select @database ,
@schema ,
@tabela ,
@indice ,
avg_fragmentation_in_percent ,
page_count
from sys.dm_db_index_physical_stats(@dbid,
@tableid,
@indexid, null,
'limited')
where avg_fragmentation_in_percent >= 20
and page_count > 10
if ( @frag >= 20 )
set @cmd = 'alter index ['+@indice+'] on ['+@database+'].
['+@schema+'].['+@tabela+'] Rebuild'
else
set @cmd = 'alter index ['+@indice+'] on ['+@database+'].
['+@schema+'].['+@tabela+'] Reorganize'
begin try
print CONVERT(VARCHAR, GETDATE(), 103) + ' '
+ convert(varchar, getdate(), 108)
+ ' rebuild do indice: [' + @indice
+ '] da tabela: [' + @tabela + ']'
exec (@cmd)
--print @cmd
end try
begin catch
print '--------- O rebuild do indice: ' + @indice
+ ' apresentou o erro: '
print error_message()
end catch