--重建索引 DECLARE @DBCCString NVARCHAR(1000) DECLARE @TableName VARCHAR(150) declare @n int declare @cu int declare @beginTime datetime declare @endTime datetime select @n= (select count(1) from sysobjects WHERE xType='U' and name not like 'tmp%') select @cu=1 DECLARE Cur_Index CURSOR FOR SELECT Name AS TblName FROM sysobjects WHERE xType='U' and name not like 'tmp%' ORDER BY TblName FOR READ ONLY OPEN Cur_Index FETCH NEXT FROM Cur_Index INTO @TableName WHILE @@FETCH_STATUS=0 BEGIN SET @DBCCString = 'DBCC DBREINDEX(@TblName,'''')WITH NO_INFOMSGS' SET @beginTime=(select getdate()) EXEC SP_EXECUTESQL @DBCCString,N'@TblName VARCHAR(180)',@TableName SET @endTime=(select getdate()) PRINT '第' + CONVERT(varchar (12), @cu)+ ' /共 ' +CONVERT(varchar (12), @n) + ' 重建表: ' + @TableName +' 的索引完成,花费时间:' + CONVERT(varchar(36) , @endTime-@beginTime, 114) FETCH NEXT FROM Cur_Index INTO @TableName select @cu=@cu+1 END CLOSE Cur_Index DEALLOCATE Cur_Index --更新统计信息 exec sp_updatestats; PRINT '操作完成!'
本文为宁若水!原创文章,转载无需和我联系,但请注明来自[若水]博客 www.lalaya.net