4

是否有任何工作方法可以修改数据库中所有表的主键的填充因子参数?

4

1 回答 1

9

该脚本查找所有主键,并为每个主键构建一个 ALTER INDEX 语句并执行它。

---- change this to your desired value ----
DECLARE @NewFillFactor smallint = 92
-------------------------------------------
DECLARE @TableName varchar(300), @IndexName varchar(300), @sql varchar(max)

DECLARE inds CURSOR FAST_FORWARD FOR
SELECT TABLE_NAME, CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE='PRIMARY KEY'

OPEN inds
FETCH NEXT FROM inds INTO @TableName, @IndexName

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'ALTER INDEX [' + @IndexName + '] ON [dbo].[' + @TableName + '] REBUILD WITH ( FILLFACTOR = ' + cast(@NewFillFactor as varchar(3)) + ')'
    PRINT @sql
    EXEC(@sql)
    FETCH NEXT FROM inds INTO @TableName, @IndexName
END

CLOSE inds
DEALLOCATE inds

PRINT 'Done'
于 2012-10-17T20:39:45.847 回答