Search
 
SCRIPT & CODE EXAMPLE
 

SQL

rebuild index table sql script

ALTER INDEX ALL ON [table_name] REBUILD
Comment

rebuild index sql server

In Object Explorer, Expand the database that contains the table on which you want to reorganize an index.
Expand the Tables folder.
Expand the table on which you want to reorganize an index.
Expand the Indexes folder.
Right-click the index you want to reorganize and select Rebuild.
Comment

Rebuild Index SQL Server

DECLARE @Database NVARCHAR(255)   
    DECLARE @Table NVARCHAR(255)  
    DECLARE @cmd NVARCHAR(1000)  

    DECLARE DatabaseCursor CURSOR READ_ONLY FOR  
    SELECT name FROM master.sys.databases   
    WHERE name IN ('YOUR DATABASE NAME')  -- databases
    AND state = 0 -- database is online
    AND is_in_standby = 0 -- database is not read only for log shipping
    ORDER BY 1  

    OPEN DatabaseCursor  

    FETCH NEXT FROM DatabaseCursor INTO @Database  
    WHILE @@FETCH_STATUS = 0  
    BEGIN  

       SET @cmd = 'DECLARE TableCursor CURSOR READ_ONLY 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
          BEGIN TRY   
             SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD' 
             PRINT @cmd -- uncomment if you want to see commands
             EXEC (@cmd) 
          END TRY
          BEGIN CATCH
             PRINT '---'
             PRINT @cmd
             PRINT ERROR_MESSAGE() 
             PRINT '---'
          END CATCH

          FETCH NEXT FROM TableCursor INTO @Table   
       END   

       CLOSE TableCursor   
       DEALLOCATE TableCursor  

       FETCH NEXT FROM DatabaseCursor INTO @Database  
    END  
    CLOSE DatabaseCursor   
    DEALLOCATE DatabaseCursor
Comment

PREVIOUS NEXT
Code Example
Sql :: soql update query 
Sql :: how to relationship query two different tables in MySQL 
Sql :: mysql is odd 
Sql :: how to get column name in db from an sqlalchemy attribute model 
Sql :: sql server remove 0 from left 
Sql :: sql default constraint 
Sql :: display first three characters sql 
Sql :: remove all spaces from string sql 
Sql :: distinct in sql 
Sql :: sqlite update query python 
Sql :: DELETE DUPLICATE VALUES FROM A TABLE IN SQL SERVER 
Sql :: sql exemplos 
Sql :: select only distinct values from another table and excluding from current table 
Sql :: date format in oracle 
Sql :: create-table 
Sql :: insert multiple rows from another table sql 
Sql :: mysql sublime build system 
Sql :: unique sql 
Sql :: SQL DATEDIFF(date_part, start_date, end_date) 
Sql :: if role exists sql 
Sql :: Postgres format number to 2 decimal places 
Sql :: postgres drop all tables 
Sql :: full-text index mysql 
Sql :: mssql unique key accept nulls 
Sql :: sql where clause 
Sql :: java.sql.sqlexception: the url cannot be null 
Sql :: oracle sql get value from several rows and concatenate strings 
Sql :: cross join sl 
Sql :: mysql set column equal to another automatic 
Sql :: connectionString 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =