SET SERVEROUTPUT ON;
BEGIN
   FOR cur_rec IN (SELECT owner,
                          object_name,
                          object_type,
                          DECODE(object_type, 'PACKAGE', 1,
                                              'PACKAGE BODY', 2, 
                                              'TRIGGER', 3, 3) AS recompile_order
                   FROM dba_objects
                   WHERE object_type IN ('PACKAGE', 'PACKAGE BODY', 'TRIGGER')
                   AND status != 'VALID'
                   AND owner = 'KFM'
                   ORDER BY 4)
   LOOP
     BEGIN
       IF cur_rec.object_type = 'PACKAGE' THEN
         EXECUTE IMMEDIATE 'ALTER ' || cur_rec.object_type || 
             ' "' || cur_rec.owner || '"."' || cur_rec.object_name || '" COMPILE';
       ELSIF cur_rec.object_type = 'PACKAGE BODY' THEN
         EXECUTE IMMEDIATE 'ALTER PACKAGE "' || cur_rec.owner || 
             '"."' || cur_rec.object_name || '" COMPILE BODY';       
       ELSIF cur_rec.object_type = 'TRIGGER' THEN
         EXECUTE IMMEDIATE 'ALTER ' || cur_rec.object_type || 
             ' "' || cur_rec.owner || '"."' || cur_rec.object_name || '" COMPILE';       
       END IF;

     EXCEPTION
       WHEN OTHERS THEN
		DBMS_OUTPUT.put_line('Ошибка компиляции ' || cur_rec.object_type || ' для ' 
			|| cur_rec.owner || '.' || cur_rec.object_name);
     END;
   END LOOP;
END;
/
