重建当前数据库中的所有索引
declare @sql nvarchar(500) declare rebuildalltableindexes cursorread_onlyfor select quotename(schema_name(tbl.schema_id)) + '.' + quotename(tbl.name) tb_name, quotename(i.name) idx_namefrom sys.tables as tblinner join sys.indexes as i on ( i.index_id > 0 and i.is_hypothetical = 0 ) and (i.object_id = tbl.object_id) declare @tb_name nvarchar(255), @idx_name nvarchar(255)open rebuildalltableindexes fetch next from rebuildalltableindexes into @tb_name, @idx_namewhile (@@fetch_status -1)begin if (@@fetch_status -2) begin set @sql = n'alter index ' + @idx_name + n' on ' + @tb_name + n' rebuild partition = all with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, online = off, allow_row_locks = on, allow_page_locks = on, fillfactor = 90)' exec sp_executesql @sql end fetch next from rebuildalltableindexes into @tb_name, @idx_nameend close rebuildalltableindexesdeallocate rebuildalltableindexesgo
