oracle String類型轉換成blob類型插入

來源:互聯網
上載者:User

CERT_1以前是varchar(3000),現在的類型是blob類型,做資料庫遷移時,使用匯入工具不能直接匯入,所以使用SQL,下面是SQL

 

 

declare
  directions      BLOB;
  amount          BINARY_INTEGER;
  offset          INTEGER;
  first_direction VARCHAR2(3000);
  more_directions VARCHAR2(3000);

  CURSOR c_TB1 Is
 
  (
    Select CERT_ID as ids, CERT_1 as blb From uaapra.tb_user_cert tb1 where CERT_1 is not null);
    v_ids integer ;
begin

  OPEN c_TB1;
  LOOP 
    fetch c_TB1
      into v_ids, first_direction;
      update tb_user_cert set cert_1  = empty_blob() where cert_id  = v_ids; --更新和新增一樣要將BLOB欄位設定為EMPTY_BLOB()
      select cert_1  into directions from tb_user_cert where cert_id  = v_ids for update; --一定要用for update鎖住記錄,否則        
      --DBMS_LOB.OPEN會出錯   
      DBMS_LOB.OPEN(directions, DBMS_LOB.LOB_READWRITE);   
      amount := LENGTHB(first_direction); --number of characters to write
      --有中文必須用LENGTHB
      offset := 1; --begin writing to the first character of the CLOB
      DBMS_LOB.WRITE(directions,
                     amount,
                     offset,
                     UTL_RAW.cast_to_raw(first_direction));
      --UTL_RAW.cast_to_raw函數將字串轉換成位元   
      DBMS_LOB.CLOSE(directions);   
      EXIT WHEN c_TB1%NOTFOUND;   
    END LOOP;
    COMMIT;
    CLOSE c_TB1;
  end;
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.