Thanks a lot
-- query in one select
exec sp_MSforeachdb @command1= '
if ''?'' not in (''master'',''model'',''msdb'',''tempdb'')
begin
use [?]
create table #indtab (ID SMALLINT IDENTITY(1,1), REBUILDSTMT nvarchar(600))
insert into #indtab
SELECT
CASE WHEN avg_fragmentation_in_percent >...