Technical Article

Reorganize indexes

,

Generate script to reorganize indexes in a database

---Objetivo: Generate script to reorganize indexes in a database
---Fecha: 09/04/2014
---Autor:  Andrés Noé Michaca Trujillo
 USE_[Database] --- BASE DE DATOS
 GO
 declare @name     nvarchar(100)
 DECLARE @sql      NVARCHAR(1000)  -- Comando SQL
 declare cursor_indices cursor for
 select 'ALTER INDEX  [' + ltrim(rtrim(ind.name))+'] on [' + ltrim(rtrim(esq.name))+'].['+ltrim(rtrim(OBJECT_NAME(ind.object_id))) +']   REORGANIZE WITH ( LOB_COMPACTION = ON )'
 from sys.indexes ind
 inner join sys.sysobjects obj on
 obj.id =ind.object_id
 inner join sys.schemas esq on 
 esq.schema_id =obj.uid
 where obj.type='U' and ind.name is not null and upper(OBJECT_NAME(ind.object_id))<> 'SYSDIAGRAMS'
 order by OBJECT_NAME(ind.object_id)
 open cursor_indices
 fetch cursor_indices into @sql
 WHILE @@FETCH_STATUS = 0
    Begin
        print @SQL
        print 'go'
        fetch cursor_indices into @sql
    end
  close cursor_indices
  deallocate cursor_indices

Rate

2 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

2 (2)

You rated this post out of 5. Change rating