SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to REBUILD all indexes in database with FILLFACTOR?

Execute the following Microsoft SQL Server T-SQL database administration script in SSMS Query Editor to REBUILD all indexes in the AdventureWorks2008 sample database.

-- SQL Server 2008 script to REBUILD all indexes for all tables

USE AdventureWorks2008;

DECLARE @DatabaseName SYSNAME   = DB_NAME(),  @TableName VARCHAR(256) 

DECLARE @FILLFACTOR INT = 85

DECLARE @DynamicSQL NVARCHAR(max) =

 'DECLARE curAllTablesInDB CURSOR FOR SELECT TABLE_SCHEMA +

 ''.'' + TABLE_NAME AS TABLENAME  

 FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.TABLES WHERE

 TABLE_TYPE = ''BASE TABLE'''  

 

BEGIN 

  EXEC sp_executeSQL @DynamicSQL  -- create tables cursor

  OPEN curAllTablesInDB  

  FETCH NEXT FROM curAllTablesInDB INTO @TableName  

  WHILE (@@FETCH_STATUS = 0) 

  BEGIN  

       SET @DynamicSQL = 'ALTER INDEX ALL ON ' + @TableName +

         ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR,@FILLFACTOR) + ')' 

       PRINT @DynamicSQL

       -- ALTER INDEX ALL ON Purchasing.ShipMethod REBUILD WITH (FILLFACTOR = 85)

       EXEC sp_executeSQL @DynamicSQL 

       FETCH NEXT FROM curAllTablesInDB INTO @TableName  

   END   -- cursor WHILE

   CLOSE curAllTablesInDB  

   DEALLOCATE curAllTablesInDB 

END

------------

-- INDEX REBUILD ONLINE

USE AdventureWorks2008;

GO

CREATE PROC sprocDBIndexREBUILD @FILLFACTOR INT = 90

AS

BEGIN

DECLARE @DatabaseName SYSNAME   = DB_NAME(),  @TableName VARCHAR(256) 

DECLARE @DynamicSQL NVARCHAR(max) =

 'DECLARE curAllTablesInDB CURSOR FOR SELECT TABLE_SCHEMA +

 ''.'' + TABLE_NAME AS TABLENAME  

 FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.TABLES WHERE

 TABLE_TYPE = ''BASE TABLE'''  

BEGIN 

  EXEC sp_executeSQL @DynamicSQL  -- create tables cursor

  OPEN curAllTablesInDB  

  FETCH NEXT FROM curAllTablesInDB INTO @TableName  

  WHILE (@@FETCH_STATUS = 0) 

  BEGIN  

       SET @DynamicSQL = 'ALTER INDEX ALL ON ' + @TableName +

         ' REBUILD WITH (ONLINE=ON, FILLFACTOR = ' + CONVERT(VARCHAR,@FILLFACTOR) + ')' 

       -- PRINT @DynamicSQL

       EXEC sp_executeSQL @DynamicSQL 

       FETCH NEXT FROM curAllTablesInDB INTO @TableName  

   END   -- cursor WHILE

   CLOSE curAllTablesInDB  

   DEALLOCATE curAllTablesInDB 

END

END -- sproc

GO

 

Related articles:

SQL SERVER – ReIndexing Database Tables and Update Statistics on Tables

SQL Server script to rebuild all indexes for all tables and all databases

http://www.sqlusa.com/bestpractices2008/performancetuning/missingindexes/

SQL Server Backup, Integrity Check, and Index and Statistics Maintenance

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.