5

How to truncate any table using its synonym in oracle?

-- in Server_A
Create Table table_a ( col int);

-- in server_B
CREATE SYNONYM syn_table_a FOR table_a@dblink_server_a;

--insert into
INSERT INTO syn_table_a values (1);

--Truncate
How to truncate table using synonym only?.
touchchandra
  • 1,506
  • 5
  • 21
  • 37

3 Answers3

3

A truncate statement cannot be used on a synonym.

Synonyms cannot be used in a drop table, drop view or truncate table/cluster statements. If this is tried, it results in a ORA-00942: table or view does not exist

For example,

SQL> CREATE TABLE t(col NUMBER);

Table created.

SQL>
SQL> CREATE SYNONYM t_syn FOR t;

Synonym created.

SQL>
SQL> TRUNCATE TABLE t_syn;
TRUNCATE TABLE t_syn
               *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL>
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
2

You could use dynamic SQL to do it, e.g.:

declare
  d varchar2(1000);
begin
  select 'TRUNCATE TABLE "' || table_owner || '"."' || table_name || '"'
  into d
  from all_synonyms
  where synonym_name = 'MYSYNONYM';
  execute immediate d;
end;

If the table is accessed via a database link, this will not work. In that case, you could create a procedure on the remote instance that does the truncate, then call that procedure across the database link, e.g.

begin
  truncate_my_table@dblinkname;
end;
Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
-1

In Oracle, you can also get ORA-14410 while trying to drop/truncate a table using synonym.

The alert log:

ORA-00604: error occurred at recursive SQL level 1 ORA-14410: RPI LOCK TABLE issued to table referenced through synonym

Follow the above dynamic SQl to drop/truncate it.

abarisone
  • 3,707
  • 11
  • 35
  • 54