Sunday, July 27, 2008

SQL Server Central: SQL 2005 IndexDefrag solution

SQL 2005 IndexDefrag solution - SQL Server Central
set @strIndex=(select IndexName from ##ADMIN where ID2=@i)
set @strSql=(@strpre+@strTable+@strmid+@strIndex+@strEnd)

--set @strSql=@strSql+ @strIndex+') ' 
--print(@strIndex) print @i

--print @strSql
if 2<= datepart(hh,convert(datetime,getdate(),121))
and 8>datepart(hh,convert(datetime,getdate(),121))
begin
exec (@strSql);
if @@error<>0
begin
set @err=0
end
print (@i)print (@err)Print(@start)print(@strTable)
insert into aa.dbo.ZZDataBaseAdminLog (ID2,tablename,ilog) values (@i,@strTable+' '+@strIndex, @err)


end
set @i=@i+1
end 


end







/*

drop table ##Admin
go
SELECT OBJECT_NAME(i.object_id) AS TableName,
i.name AS IndexName,indexstats.*,
indexstats.avg_fragmentation_in_percent as FRAG
into ##Admin
FROM sys.dm_db_index_physical_stats(9, NULL, NULL, NULL, 'Limited') indexstats
INNER JOIN sys.indexes i ON i.object_id = indexstats.object_id
AND i.index_id = indexstats.index_id


where i.name is not null
order by indexstats.avg_fragmentation_in_percent desc

select * from ##admin

select top 10 * from sys.indexes
*/



No comments: