declare @dbccstring nvarchar(1000)
declare @tablename varchar(100)
declare cur_index cursor
for
select name as tblname
from sysobjects
where xtype='u'
order by tblname
for read only
open cur_index
fetch next from cur_index
into @tablename
while @@fetch_status=0
begin
print '重建表'+@tablename +'的索引……start!'
set @dbccstring='dbcc dbreindex(@tblname,'''')with no_infomsgs'
exec sp_executesql @dbccstring,
N'@tblname varchar(100)',
@tablename
print '重建表'+@tablename+'的索引……end!'
fetch next from cur_index
into @tablename
end
close cur_index
deallocate cur_index
print '操作完成!'