define p_owner='&1.'
col objeto format a40
set serverout on verify off
spool checksym.&P_OWNER..log

declare
  e_sym_invalid exception;

  cursor c1 is
    SELECT o.status, s.owner, s.synonym_name, table_owner || '.' || table_name objeto
    FROM dba_synonyms s, dba_objects o
    WHERE o.object_name = s.synonym_name
    and o.owner = s.owner
    and o.object_type = 'SYNONYM'
    and o.status = 'INVALID'
    and s.owner = upper( '&p_owner.');
    --and rownum < 3;
    --and s.table_owner = 'ADMCAD';

  cStmCompile VARCHAR2(200);
  cStmRebuild VARCHAR2(200);
  cStmDrop    VARCHAR2(200);

  pragma exception_init( e_sym_invalid, -980 );
begin
  dbms_output.enable(1e+6);
  for r in c1 loop
    if r.owner = 'PUBLIC' then
      cStmRebuild := 'create public synonym ' || r.synonym_name || ' for ' || r.objeto;
      cStmCompile := 'alter public synonym ' || r.synonym_name || ' compile';
      cStmDrop    := 'drop public synonym ' || r.synonym_name;
    else
      cStmRebuild := 'create synonym ' || r.owner ||'.'|| r.synonym_name || ' for ' || r.objeto;
      cStmCompile := 'alter synonym ' || r.owner ||'.'|| r.synonym_name || ' compile';
      cStmDrop    := 'drop synonym ' || r.owner ||'.'|| r.synonym_name;
    end if;

    begin
      execute immediate cStmCompile;
    exception
      when e_sym_invalid then
        dbms_output.put_line( cStmRebuild );
        begin
          execute immediate cStmDrop;
        exception
          when others then
            dbms_output.put_line( cStmDrop );
        end;
      when others then
        dbms_output.put_line( sqlerrm );
        dbms_output.put_line( cStmCompile );
    end;
  end loop;
end;
/

SELECT o.status, table_owner, count(*)
FROM dba_synonyms s, dba_objects o
WHERE o.object_name = s.synonym_name
and o.owner = s.owner
and o.object_type = 'SYNONYM'
and o.status = 'INVALID'
and s.owner = upper( '&p_owner.')
group by status, table_owner;

spool off


hostgator