Skip to content

Commit 5366e87

Browse files
authored
Create rebuild_all_index_in_db.sql
1 parent ed3a7c6 commit 5366e87

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
USE University;
2+
GO
3+
4+
DECLARE @TableName NVARCHAR(255);
5+
DECLARE @SQL NVARCHAR(MAX);
6+
7+
DECLARE table_cursor CURSOR FOR
8+
SELECT QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name)
9+
FROM sys.tables t
10+
WHERE t.is_ms_shipped = 0;
11+
12+
OPEN table_cursor;
13+
FETCH NEXT FROM table_cursor INTO @TableName;
14+
15+
WHILE @@FETCH_STATUS = 0
16+
BEGIN
17+
SET @SQL = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = 90, SORT_IN_TEMPDB = ON);';
18+
PRINT 'Rebuilding indexes on: ' + @TableName;
19+
EXEC sp_executesql @SQL;
20+
FETCH NEXT FROM table_cursor INTO @TableName;
21+
END;
22+
23+
CLOSE table_cursor;
24+
DEALLOCATE table_cursor;

0 commit comments

Comments
 (0)