
USE [您的数据库名]
GO

DECLARE @TableName VARCHAR(255)
DECLARE @SQL NVARCHAR(500)
DECLARE @FillFactor INT = 90 -- 填充因子设置为90%

DECLARE TableCursor CURSOR FOR
SELECT table_name 
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @SQL = 'ALTER INDEX ALL ON [' + @TableName + '] REBUILD WITH (FILLFACTOR = ' + CAST(@FillFactor AS VARCHAR(3)) + ')'
    PRINT '正在重建表: ' + @TableName
    EXEC sp_executesql @SQL
    FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor
DEALLOCATE TableCursor
PRINT '所有表索引重建完成'
GO
