Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Generate script to rebuild indexes in a database

By Andrés Michaca,

Generate script for reconstgruir ena database indexes

---Objetivo: RECONSTRUYE INDICES DE UNA BASE DE DATOS
---Fecha: 19/03/2014
---Autor:  Andrés Noé Michaca Trujillo
select 'ALTER INDEX  [' + ltrim(rtrim(ind.name))+'] on [' + ltrim(rtrim(esq.name))+'].['+ltrim(rtrim(OBJECT_NAME(ind.object_id))) +']  REBUILD PARTITION  =  ALL  WITH  ('+
    ltrim(rtrim(case  ind.is_padded
                   when  1 then ' PAD_INDEX = ON, '
                   else ' PAD_INDEX = OFF, '
                end
    )   )+
    ' STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, '+
    ltrim(rtrim(case
                   when ind.is_unique =0  and ignore_dup_key= 1 then ' IGNORE_DUP_KEY = ON, '
       when ind.is_unique =0  and ignore_dup_key= 0 then ' IGNORE_DUP_KEY = OFF, '
                   else ''
           end
   )  )
    +'ONLINE = OFF,'+
    ltrim(rtrim(case ind.allow_row_locks
                     when 1 then ' ALLOW_ROW_LOCKS = ON, '
                     else ' ALLOW_ROW_LOCKS = OFF, '
                 end
   )  ) +
    ltrim(rtrim(case ind.allow_page_locks
                     when 1 then ' ALLOW_PAGE_LOCKS = ON)'
                    else ' ALLOW_PAGE_LOCKS = OFF)'
                end
   )   )    
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)

Total article views: 1333 | Views in the last 30 days: 51
 
Related Articles
FORUM

LTRIM and RTRIM.

LTRIM and RTRIM.

FORUM

Ltrim rtrim no working with UPDATE

Ltrim rtrim no working with UPDATE

FORUM

LTRIM,RTRIM

hi i have this code in where clause RTRIM(LTRIM(ODER_ID))= ' ',WHAT THis do [quote] i have this ...

FORUM

speed up LEN(RTRIM(LTRIM(ERR))) > 0????

ERR is a column of char(255)   How can I speed up this reference?   LEN(RTRIM(LTRIM(ERR))) > 0 ...

FORUM

ANYTHING THAT CAN HELP TRIM THE DATA FOR A GIVEN STRING...

LTRIM AND RTRIM Doesn't help ...

Tags
indexing    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones