Here's a script I put together that you can test out. You may have to tweak it.
Run it against a test database and try it out.
set nocount on
go
if object_id('tempdb..#table') is not null
drop table #table
go
if object_id('tempdb..#dbcc') is not null
drop table #dbcc
go
declare @counter int, @totalrows int, @tablename sysname
create table #table (tempid int identity not null primary key, tablename sysname)
create table #dbcc (dbccid int identity not null primary key, objectname sysname, objectid int, indexname sysname, indexid int, level int,
pages int, rows int, minRecordSize int, MaxRecordSize int, AvgRecordSize int, ForwardedRecords int, Extents int,
ExtentSwitches int, AvgFreeBytes int, AvgPageDensity int, ScanDensity int, BestCount int, ActualCount int,
LogicalFragmentation int, ExtentFragmentation int)
insert into #table (tablename)
select table_name
from information_schema.tables
where table_type = 'base table'
and objectproperty(object_id(table_name),'IsMSShipped') = 0
select @totalrows = @@rowcount
-- Load the fragmentation data for each index
select @counter = 1
while (@counter <= @totalrows)
begin
select @tablename = tablename
from #table
where tempid = @counter
insert into #dbcc (objectname, objectid, indexname, indexid, [level], pages, [rows], minRecordSize, MaxRecordSize, AvgRecordSize,
ForwardedRecords, Extents, ExtentSwitches, AvgFreeBytes, AvgPageDensity, ScanDensity, BestCount, ActualCount,
LogicalFragmentation, ExtentFragmentation)
exec ('dbcc showcontig (' + @tablename + ') with tableresults, all_indexes')
-- prepare to get the next index
select @counter = @counter + 1
end
select top 10 indexname, rows, logicalfragmentation, bestcount, actualcount
from #dbcc
where actualcount > 0
and indexname <> ''
--and pages > 1000
order by logicalfragmentation desc
go