Oracle數據鏈創建及使用

 

項目上需要將老系統中的數據導入到新系統中,決定用數據鏈dblink將老數據導入到目標數據庫中,將操作過程記錄如下:

1.創建Dblink

  create database link ygbgtest_portaltest_link
    connect to dbuser identified by password
    using '(DESCRIPTION =
     (ADDRESS_LIST =
       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.xx.xx)(PORT = 1521))
     )
     (CONNECT_DATA =
       (SERVICE_NAME = orcl)
     )
   )';

2.用鏈表查詢

 執行SQL  select * from ygbgtest_portaltest_link@portal_information; 

 報錯“ORA-02019:未找到遠程數據庫的連接說明”。檢查發現表名和數據鏈名寫反了,⊙﹏⊙,調整後執行 select * from portal_information@ygbgtest_portaltest_link;

 報錯“ORA-22992:無法使用從遠程表選擇的LOB定位符”。檢查發現報錯原因是查詢的源數據表中含有CLOB類型字段。 

3.解決dblink查詢源數據表中含有大字段的問題

我解決該問題的方法是通過創建臨時表,並將源數據表中的數據導入到臨時表中。然後查詢臨時表以獲取CLOB字段數據。

--創建臨時表以獲取遠程表數據   
create global temporary table temp_ygbg_information on commit preserve rows 
as select * from portal_information@ygbgtest_portaltest_link;
select count(1from temp_ygbg_information t;

 

--從臨時表中將數據插入到目的表中
insert into portal_information
  (id,
   title,
   picture_url,
   status,
   author_id,
   author_name,
   create_time,
   modify_date,
   delete_date,
   view_num,
   order_flag,
   summary,
   type,
   promulgation_charge,
   information_source,
   sort_num,
   sub_title,
   is_slidenews)
 select 
   SEQ_PORTAL_INFORMATION.NEXTVAL,
   title,
   picture_url,
   status,
   author_id,
   author_name,
   create_time,
   modify_date,
   delete_date,
   view_num,
   order_flag,
   summary,
   type,
   promulgation_charge,
   information_source,
   sort_num,
   sub_title,
   is_slidenews from temp_ygbg_information t1 where t1.id=3338;

 

--查看大字段中的數據 
select dbms_lob.substr(t.summary,4000,1) ty,t.* from portal_information t where t.id=3338;

 

 

自此,通過Dblink查詢和獲取源數據庫中的表數據並插入到目標數據庫中的操作均能正常執行了。當然網上還有其它辦法可以查看大字段,例如使用視圖等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章