DECLARE @tablename VARCHAR(50)
DECLARE @indexname VARCHAR(50)
DECLARE @cmdsql NVARCHAR(MAX)
DECLARE index_cursor CURSOR
FOR
SELECT OBJECT_NAME(object_id) AS Table_Name ,
name
FROM sys.indexes
WHERE name IS NOT NULL
AND OBJECT_NAME(object_id) IN ( SELECT name
FROM sys.tables )
OPEN index_cursor
FETCH NEXT FROM index_cursor
INTO @tablename,@indexname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmdsql ='ALTER INDEX ['+@indexname+'] ON [dbo].['+@tablename+'] REBUILD WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, SORT_IN_TEMPDB = OFF, ONLINE = OFF )'
EXECUTE sp_executesql @cmdsql
PRINT @cmdsql
FETCH NEXT FROM index_cursor
INTO @tablename,@indexname
END
CLOSE index_cursor
DEALLOCATE index_cursor
轉載地址:https://www.cnblogs.com/amylis_chen/p/3369451.html