gpt4 book ai didi

sql-server - 修改 MS SQL 2008 R2 中所有表中主键的填充因子

转载 作者:行者123 更新时间:2023-12-01 10:03:54 26 4
gpt4 key购买 nike

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

最佳答案

此脚本查找所有主键,并为每个主键构建一个 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'

关于sql-server - 修改 MS SQL 2008 R2 中所有表中主键的填充因子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12814100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com