这是我使用游标创建的脚本,用于为具有MSSQL2008索引的任何表重建和填充全索引。这在具有从MSSQL 2000服务器迁移的数据库的生产环境中工作。我已经关闭了更改跟踪,只是通过SQL Server代理运行此存储过程。如果您使用Express,则可以使用VBS脚本通过Task Scheduler运行它。
在脚本中重要的是在尝试填充索引之前,首先在每个目录上进行重建。
CREATE PROCEDURE [dbo].[rebuild_repopulate_fulltext]
AS
BEGIN
Declare @cmdA NVARCHAR(255)
Declare @cmdB NVARCHAR(255)
Declare @cmdC NVARCHAR(255)
DECLARE @Database VARCHAR(255)
DECLARE @Table VARCHAR(255)
DECLARE @cmd NVARCHAR(500)
DECLARE @fillfactor INT
DECLARE @Catalog VARCHAR(255)
DECLARE @Schema VARCHAR(255)
SET @fillfactor = 90
DECLARE DatabaseCursor CURSOR FOR
SELECT name FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','msdb','tempdb','model','distribution')
ORDER BY 1
OPEN DatabaseCursor
FETCH NEXT FROM DatabaseCursor INTO @Database
WHILE @@FETCH_STATUS = 0
BEGIN
-- rebuild fulltext catalog
set @cmd = 'DECLARE CatalogCursor CURSOR FOR
SELECT t.name AS TableName, c.name AS FTCatalogName, s.name as schemaname
FROM ['+ @Database + '].sys.tables t JOIN ['+ @Database +'].sys.fulltext_indexes i
ON t.object_id = i.object_id
JOIN ['+ @Database + '].sys.fulltext_catalogs c
ON i.fulltext_catalog_id = c.fulltext_catalog_id
JOIN ['+ @Database + '].sys.schemas s ON t.schema_id = s.schema_id'
--PRINT @cmd
EXEC (@cmd)
OPEN CatalogCursor
FETCH NEXT FROM CatalogCursor INTO @Table, @Catalog, @Schema
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmdB = 'USE ['+ @Database + ']; ALTER FULLTEXT CATALOG ' + @Catalog + ' REBUILD;'
--PRINT @cmdB
EXEC (@cmdB)
FETCH NEXT FROM CatalogCursor INTO @Table, @Catalog, @Schema
END
CLOSE CatalogCursor
OPEN CatalogCursor
FETCH NEXT FROM CatalogCursor INTO @Table, @Catalog, @Schema
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmdC = 'USE ['+ @Database + ']; ALTER FULLTEXT INDEX ON ['+ @Database + '].[' + @Schema + '].[' + @Table + '] START FULL POPULATION;'
--PRINT @cmdC
EXEC (@cmdC)
FETCH NEXT FROM CatalogCursor INTO @Table, @Catalog, @Schema
END
CLOSE CatalogCursor
DEALLOCATE CatalogCursor
FETCH NEXT FROM DatabaseCursor INTO @Database
END
CLOSE DatabaseCursor
DEALLOCATE DatabaseCursor
END
有没有人有不需要游标的方法?