Skip to Main Content
  • Questions
  • Script to recompile the synonyms in a schema

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: February 21, 2017 - 7:37 pm UTC

Last updated: September 07, 2017 - 2:25 am UTC

Version: 11G

Viewed 50K+ times! This question is

You Asked

Hi

I have written the below script to recompile the synonyms in all the schemas but I am getting invalid character error. Could you check is there any wrong with the script:

spool 'c:synonyms.txt'

begin

for i in (select object_name,owner from all_objects
where owner like '&1.%'
and object_type = 'SYNONYM'
and status = 'INVALID')
LOOP
begin
execute immediate 'alter synonym ' || i.owner || '.' || i.object_name || ' compile ; ';
exception when others then
dbms_output.put_line ( 'unable to recompile '||i.owner||'.'||i.object_name||' '||' -ERROR- '||SQLERRM);
end;
end loop;
end;
/

SPOOL OFF.

I am calling the script by passing the variable value.

The error is as below:

unable to recompile BASE_CORE.PARTICIPANT_AWARD -ERROR- ORA-00911: i
nvalid character


and Connor said...

1) execute immediate does not need a semi-column - thats a sqlplus thing

2) you should take care of mixed case


Try making your DDL as

execute immediate 'alter synonym "' || i.owner || '"."' || i.object_name || '" compile';

and see how that cause.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, September 06, 2017 - 3:08 pm UTC

more easy I think... the synonyms are recompiled automatically after execution a select to it...

SQL> spool kk.sql

SQL> select 'select * from '||owner||'.'||object_name||' where 1=0;' from dba_objects where status='INVALID' and object_type='SYNONYM' order by 1;
.
.
.
SQL> spool off
SQL> !vi kk.sql

SQL> @kk


SQL> select 'select * from '||owner||'.'||object_name||' where 1=0;' from dba_objects where status='INVALID' and object_type='SYNONYM' order by 1;

no rows selected
Connor McDonald
September 07, 2017 - 2:25 am UTC

nice input

Joao Amaral, July 23, 2019 - 12:32 pm UTC

select 'alter synonym '||owner||'.'||object_name||' compile; ' from dba_objects where status='INVALID' and object_type='SYNONYM' and owner='USER' order by 1;


select 'alter synonym '||owner||'.'||object_name||' compile; ' from dba_objects where status='INVALID' and object_type='SYNONYM' order by 1;

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here