Skip to Main Content
  • Questions
  • PUBLIC SYNONYM is not compiling using execute immediate

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Walter.

Asked: July 31, 2018 - 7:17 pm UTC

Last updated: August 21, 2018 - 11:20 am UTC

Version: 12c

Viewed 1000+ times

You Asked

Hi There,

I am having a problem trying to compile public synonyms using a block. The block runs fine - however, it doesn't compile the public synonym. When I run the query select * from all_objects where status = 'INVALID', the public synonyms are still showing as "INVALID". When I try to compile it manually it works.

Do you know what is the problem ?

declare 
  cursor l_c is (select * from all_objects where status = 'INVALID');
  l_sql varchar2(3500);
  l_type varchar2(3500);
begin 
  for i in l_c loop
    
      if i.object_name = 'SYNONYM' and i.owner = 'PUBLIC'
      then
        l_sql := 'alter PUBLIC SYNONYM ' || i.object_name || ' compile';        
      else
        select decode(i.object_type,'PACKAGE BODY','PACKAGE',i.object_type)
          into l_type
          from dual;
          
        l_sql := 'alter ' || l_type || ' ' || i.owner || '.' || i.object_name || ' compile';
      end if;
      
      execute immediate l_sql;  
 
  end loop;
end;
/


Best Regards,
Walter

and Connor said...

Your script is wrong

SQL> create public synonym xxx for mcdonac.t;

Synonym created.

SQL> select * from all_objects where status = 'INVALID' and object_name = 'XXX';

no rows selected

SQL> drop table t;

Table dropped.

SQL> select * from all_objects where status = 'INVALID' and object_name = 'XXX';

OWNER                          OBJECT_NAME
------------------------------ ----------------------------------------
SUBOBJECT_NAME
----------------------------------------------------------------------------------------------------
 OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE             CREATED   LAST_DDL_ TIMESTAMP           STATUS  T
---------- -------------- ----------------------- --------- --------- ------------------- ------- -
G S  NAMESPACE
- - ----------
EDITION_NAME
----------------------------------------------------------------------------------------------------
SHARING            E O A
------------------ - - -
DEFAULT_COLLATION
----------------------------------------------------------------------------------------------------
D S CREATED_APPID CREATED_VSNID MODIFIED_APPID MODIFIED_VSNID
- - ------------- ------------- -------------- --------------
PUBLIC                         XXX

     96571                SYNONYM                 21-AUG-18 21-AUG-18 2018-08-21:19:16:40 INVALID N
N N          1

NONE               N N N

N N


1 row selected.

SQL> set serverout on
SQL> declare
  2    cursor l_c is (select * from all_objects where status = 'INVALID' and object_name = 'XXX');
  3    l_sql varchar2(3500);
  4    l_type varchar2(3500);
  5  begin
  6    for i in l_c loop
  7
  8        if i.object_name = 'SYNONYM' and i.owner = 'PUBLIC'
  9        then
 10          l_sql := 'alter PUBLIC SYNONYM ' || i.object_name || ' compile';
 11        else
 12          select decode(i.object_type,'PACKAGE BODY','PACKAGE',i.object_type)
 13            into l_type
 14            from dual;
 15
 16          l_sql := 'alter ' || l_type || ' ' || i.owner || '.' || i.object_name || ' compile';
 17        end if;
 18
 19        dbms_output.put_line(l_sql);
 20        execute immediate l_sql;
 21
 22    end loop;
 23  end;
 24  /
alter SYNONYM PUBLIC.XXX compile
declare
*
ERROR at line 1:
ORA-00995: missing or invalid synonym identifier
ORA-06512: at line 20
ORA-06512: at line 20


Elapsed: 00:00:00.05
SQL>



Cause - check line 8, it is not object *name* you want to be checking :-)

Rating

  (1 rating)

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

Comments

Steve, August 21, 2018 - 11:39 am UTC

And line 16 needs attention doesn't it?

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database