重建索引出现问题,急 执行查询“declare @schemaname varchar(255)
declare @tablename varchar(255)
declare @indexname varchar(255)
declare @fragmentation float
declare @command varchar(255)
declare tablecursor cursor for select SCHEMA_NAME(CAST(OBJECTPROPERTYEX(i.object_id,'schemaId')as int)),object_name(i.object_id),i.name,ps.avg_fragmentation_in_percent
from sys.dm_db_index_physical_stats(db_id(),null,null,null,null)as ps join sys.indexes as i on ps.object_id=i.object_id and ps.index_id=i.index_id where avg_fragmentation_in_percent>30
open tablecursor
while @@fetch_status=0
begin
print @schemaname+'.'+@tablename+'.'+@indexname+'is'+cast(@fragmentation as varchar)+'% fragmentented'
set @command='alter index '+rtrim(@indexname)+' on['+isnull(@schemaname,'dbo')+'.'+rtrim(@tablename)+'] rebuild'
exec (@command)
fetch next from tablecursor into @schemaname,@tablename,@indexname,@fragmentation
end
close tablecursor
deallocate tablecursor”失败,错误如下:“找不到对象 "dbo.MOCMA",因为它不存在或者您没有所需的权限。
dbo.MOCMA表确实存在,而是用SA登录的,不知道为什么,请指点? ------解决方案-------------------- 把数据库 也限定上,如 TESTdb.dbo.tablename ------解决方案-------------------- 加个use 库名在开头 ------解决方案-------------------- 好习惯
declare @schemaname varchar(255)
declare @tablename varchar(255)
declare @indexname varchar(255)
declare @fragmentation float
declare @command varchar(255)
declare tablecursor cursor for select SCHEMA_NAME(CAST(OBJECTPROPERTYEX(i.object_id,'schemaId')as int)),object_name(i.object_id),i.name,ps.avg_fragmentation_in_percent
from sys.dm_db_index_physical_stats(db_id(),null,null,null,null)as ps join sys.indexes as i on ps.object_id=i.object_id and ps.index_id=i.index_id where avg_fragmentation_in_percent>30
open tablecursor
fetch next from tablecursor into @schemaname,@tablename,@indexname,@fragmentation