DECLARE

wMB integer  := 0;
wEX integer  := 0;

wOwner VARCHAR2(255) := 'CINEMA';

indexname VARCHAR2(255);

CURSOR cIndexes  (pOwner VARCHAR2) is select owner || '.' || segment_name from dba_segments where segment_type in ('INDEX') and  owner=pOwner order by 1;

BEGIN
    
    select sum(bytes/1024) mb, SUM(EXTENTS) EXTENTS into wMB,wEX  from dba_segments where segment_type in ('TABLE','INDEX') and  owner = wOwner;

    DBMS_OUTPUT.PUT_LINE('Spazio MB :  ' || to_char(wMB) || '  EXTENTS :  ' || to_char(wEX));

    
    OPEN cIndexes(wOwner);
    LOOP
        FETCH cIndexes INTO indexname;
        EXIT WHEN cIndexes%NOTFOUND;
        
        DBMS_OUTPUT.PUT_LINE('REBUILD INDEX ' || indexname);

        BEGIN
            EXECUTE IMMEDIATE 'ALTER INDEX ' ||  indexname || ' REBUILD';
        EXCEPTION
            WHEN OTHERS THEN  DBMS_OUTPUT.PUT_LINE('ERRORE REBUILD INDEX ' || indexname);
        END;

    END LOOP;
    CLOSE cIndexes;

    select sum(bytes/1024) mb, SUM(EXTENTS) EXTENTS into wMB,wEX  from dba_segments where segment_type in ('TABLE','INDEX') and  owner = wOwner;

    DBMS_OUTPUT.PUT_LINE('Spazio MB :  ' || to_char(wMB) || '  EXTENTS :  ' || to_char(wEX));

    
END;