Reindex your tables

/****** Object:  StoredProcedure [dbo].[test]    14:39:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[test]
AS
     SET NOCOUNT ON;


DECLARE @Database VARCHAR(255)  
DECLARE @Table VARCHAR(255) 
DECLARE @cmd NVARCHAR(500) 
DECLARE @fillfactor INT


Set @Database='Sigorta';


SET @fillfactor = 90


   SET @cmd = 'DECLARE TableCursor CURSOR FOR SELECT ''['' + table_catalog + ''].['' + table_schema + ''].['' +
  table_name + '']'' as tableName FROM [' + @Database + '].INFORMATION_SCHEMA.TABLES
  WHERE table_type = ''BASE TABLE'''  


   -- create table cursor 
   EXEC (@cmd) 
   OPEN TableCursor  


   FETCH NEXT FROM TableCursor INTO @Table  
   WHILE @@FETCH_STATUS = 0  
   BEGIN  


       IF (@@MICROSOFTVERSION / POWER(2, 24) >= 9)
       BEGIN


               BEGIN TRY
                  -- SQL 2005 or higher command
                  SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'
                  EXEC (@cmd)


                    PRINT @Table ;  
               END TRY
               BEGIN CATCH
                    PRINT @Table ;
               END CATCH;
      
              
        END
       ELSE
       BEGIN
          -- SQL 2000 command
          DBCC DBREINDEX(@Table,' ',@fillfactor) 
       END


       FETCH NEXT FROM TableCursor INTO @Table  
   END  


   CLOSE TableCursor  
   DEALLOCATE TableCursor

Leave a Reply

Your email address will not be published. Required fields are marked *