如何通过dblink truncate远程数据库上的表
生活随笔
收集整理的這篇文章主要介紹了
如何通过dblink truncate远程数据库上的表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一般情況下,當我們直接truncate一個遠程的表的時候,通常會返回如下的錯誤信息:
ORA-02021: DDL operations are not allowed on a
remote database.
比如下面的示例:
先在數據庫test上創建一個test表,并插入一些數據,如下:
SQL> conn toms/toms
已連接。
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
test
SQL> create table test(no int);
表汛唇ā?
SQL> insert into test values(100);
已創建 1 行。
SQL> commit;
提交完成
然后在另外一個數據庫(study)上建一個 dblink,并嘗試去truncate test數據庫上
toms用戶下的test表:
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
study
SQL> create database link from_test connect to toms?
identified by toms using 'local_test';
數據庫鏈接已創建。
SQL> select * from toms.test@from_test;
未選定行
SQL> truncate table toms.test@from_test;
truncate table toms.test@from_test
?? ? ? ? ? ? ? ? ? ? ? ? *
ERROR 位于第 1 行:
ORA-02021: 不允許對遠程數據庫進行 DDL 操作
SQL>?
這時,我們得到了ORA-02021這樣的錯誤,Oracle不允許這么做。那么如果確實有這樣的需求,
我們該怎么辦呢。下面我介紹一個方法:
先在test數據庫上,建立一個類似如下的procedure:
SQL> create or replace procedure proc_truncate_remote_tab(p_tname in varchar2) as
??2 ?BEGIN
??3 ? ? EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || p_tname;
??4 ?EXCEPTION
??5 ? ? WHEN OTHERS THEN
??6 ? ? ? ? ?raise_application_error(-20001,SQLERRM);
??7 ?end;
??8 ?/
過程已創建。
然后在study數據庫上調用test數據庫上的這個procedure去完成這個truncate動作:
BEGIN
?? proc_truncate_remote_tab@db_link('remote_table_name');
END;
下面測試驗證一下:
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
study
SQL>?
SQL> select *from test@from_test;
?? ? ? ?NO
----------
?? ? ? 100
SQL> ?begin
??2 ? ? ?proc_truncate_remote_tab@from_test('test');
??3 ? end;
??4 ?/
PL/SQL 過程已成功完成。
SQL> select *from test@from_test;
未選定行
SQL>?
可以看到,遠程test數據庫上toms用戶下的表test已經被truncate掉了。
ORA-02021: DDL operations are not allowed on a
remote database.
比如下面的示例:
先在數據庫test上創建一個test表,并插入一些數據,如下:
SQL> conn toms/toms
已連接。
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
test
SQL> create table test(no int);
表汛唇ā?
SQL> insert into test values(100);
已創建 1 行。
SQL> commit;
提交完成
然后在另外一個數據庫(study)上建一個 dblink,并嘗試去truncate test數據庫上
toms用戶下的test表:
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
study
SQL> create database link from_test connect to toms?
identified by toms using 'local_test';
數據庫鏈接已創建。
SQL> select * from toms.test@from_test;
未選定行
SQL> truncate table toms.test@from_test;
truncate table toms.test@from_test
?? ? ? ? ? ? ? ? ? ? ? ? *
ERROR 位于第 1 行:
ORA-02021: 不允許對遠程數據庫進行 DDL 操作
SQL>?
這時,我們得到了ORA-02021這樣的錯誤,Oracle不允許這么做。那么如果確實有這樣的需求,
我們該怎么辦呢。下面我介紹一個方法:
先在test數據庫上,建立一個類似如下的procedure:
SQL> create or replace procedure proc_truncate_remote_tab(p_tname in varchar2) as
??2 ?BEGIN
??3 ? ? EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || p_tname;
??4 ?EXCEPTION
??5 ? ? WHEN OTHERS THEN
??6 ? ? ? ? ?raise_application_error(-20001,SQLERRM);
??7 ?end;
??8 ?/
過程已創建。
然后在study數據庫上調用test數據庫上的這個procedure去完成這個truncate動作:
BEGIN
?? proc_truncate_remote_tab@db_link('remote_table_name');
END;
下面測試驗證一下:
SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
study
SQL>?
SQL> select *from test@from_test;
?? ? ? ?NO
----------
?? ? ? 100
SQL> ?begin
??2 ? ? ?proc_truncate_remote_tab@from_test('test');
??3 ? end;
??4 ?/
PL/SQL 過程已成功完成。
SQL> select *from test@from_test;
未選定行
SQL>?
可以看到,遠程test數據庫上toms用戶下的表test已經被truncate掉了。
總結
以上是生活随笔為你收集整理的如何通过dblink truncate远程数据库上的表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Logdump使用指引
- 下一篇: linux cmake编译源码,linu