CLOB與BLOB的轉換
CLOB轉換為BLOB在Oracle中可以通過utl_raw和dbms_lob兩個包來實現。
1) 在utl_raw中FUNCTION cast_to_raw(c IN VARCHAR2 CHARACTER SET ANY_CS) RETURN RAW
用來把字串轉為raw。由於傳入的varchar2必然受到32767位元組長度的限制。
SQL> set serveroutput on size 2000;
SQL>
SQL> declare
2 b_blob blob;
3 c_clob varchar2(32767);
4 begin
5 for i in 1..32767 loop
6 c_clob := c_clob ||'a';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
32767
32767
PL/SQL procedure successfully completed
SQL>
SQL> declare
2 b_blob blob;
3 c_clob varchar2(32767);
4 begin
5 for i in 1..32767 loop
6 c_clob := c_clob ||'a';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
32767
32767
PL/SQL procedure successfully completed
但把 c_clob varchar2(32767) 換為 c_clob clob會怎樣呢
SQL> declare
2 b_blob blob;
3 c_clob clob;
4 begin
5 for i in 1..32767 loop
6 c_clob := c_clob ||'a';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
ORA-06502: PL/SQL: 數字或值錯誤
ORA-06512: 在 line 10
2 b_blob blob;
3 c_clob clob;
4 begin
5 for i in 1..32767 loop
6 c_clob := c_clob ||'a';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
ORA-06502: PL/SQL: 數字或值錯誤
ORA-06512: 在 line 10
SQL> declare
2 b_blob blob;
3 c_clob clob;
4 begin
5 for i in 1..16383 loop -- 修改這兒的長度(如果16384就會報錯)
6 c_clob := c_clob ||'a';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
16383
16383
PL/SQL procedure successfully completed
2 b_blob blob;
3 c_clob clob;
4 begin
5 for i in 1..16383 loop -- 修改這兒的長度(如果16384就會報錯)
6 c_clob := c_clob ||'a';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
16383
16383
PL/SQL procedure successfully completed
我們再來試試中文如何
SQL> declare
2 b_blob blob;
3 c_clob clob;
4 begin
5 for i in 1..16383 loop
6 c_clob := c_clob ||'鄧';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
16383
32766
PL/SQL procedure successfully completed
2 b_blob blob;
3 c_clob clob;
4 begin
5 for i in 1..16383 loop
6 c_clob := c_clob ||'鄧';
7 end loop;
8 dbms_output.put_line(dbms_lob.getlength(c_clob));
9 b_blob := utl_raw.cast_to_raw(c_clob);
10 dbms_output.put_line(dbms_lob.getlength(b_blob));
11 end;
12 /
16383
32766
PL/SQL procedure successfully completed
看來cast_to_raw對於clob的型別最大是支援16383字元長度(一個漢字只當一個字元長度)。
2) DBMS_LOB中
PROCEDURE convertToBlob(dest_lob IN OUT NOCOPY BLOB,
src_clob IN CLOB CHARACTER SET ANY_CS,
amount IN INTEGER,
dest_offset IN OUT INTEGER,
src_offset IN OUT INTEGER,
blob_csid IN NUMBER,
lang_context IN OUT INTEGER,
warning OUT INTEGER);
src_clob IN CLOB CHARACTER SET ANY_CS,
amount IN INTEGER,
dest_offset IN OUT INTEGER,
src_offset IN OUT INTEGER,
blob_csid IN NUMBER,
lang_context IN OUT INTEGER,
warning OUT INTEGER);
SQL> declare
2 b_blob blob;
3 c_clob clob;
4 l_lang NUMBER := DBMS_LOB.default_lang_ctx;
5 i_dest_offset number := 1;
6 i_src_offset number :=1;
7 l_warning NUMBER;
8 begin
9 DBMS_LOB.CREATETEMPORARY(b_blob,TRUE,2);
10 for i in 1 .. 40000 loop
11 c_clob := c_clob || '鄧';
12 end loop;
13 dbms_output.put_line(dbms_lob.getlength(c_clob));
14 dbms_lob.convertToBlob(b_blob,
15 c_clob,
16 dbms_lob.lobmaxsize,
17 i_dest_offset,
18 i_src_offset,
19 DBMS_LOB.default_csid,
20 l_lang,
21 l_warning);
22 dbms_output.put_line(dbms_lob.getlength(b_blob));
23 end;
24 /
40000
80000
2 b_blob blob;
3 c_clob clob;
4 l_lang NUMBER := DBMS_LOB.default_lang_ctx;
5 i_dest_offset number := 1;
6 i_src_offset number :=1;
7 l_warning NUMBER;
8 begin
9 DBMS_LOB.CREATETEMPORARY(b_blob,TRUE,2);
10 for i in 1 .. 40000 loop
11 c_clob := c_clob || '鄧';
12 end loop;
13 dbms_output.put_line(dbms_lob.getlength(c_clob));
14 dbms_lob.convertToBlob(b_blob,
15 c_clob,
16 dbms_lob.lobmaxsize,
17 i_dest_offset,
18 i_src_offset,
19 DBMS_LOB.default_csid,
20 l_lang,
21 l_warning);
22 dbms_output.put_line(dbms_lob.getlength(b_blob));
23 end;
24 /
40000
80000
結論:字元轉換為位元組:對於小於32767的字串可以使用utl_raw包;對於超長的字元必須使用dbms_lob包.
注:要計算CLOB的位元組長度,要先轉換為blob型別,再用dbms_lob.getlength來計算長度.
lengthb是不支援CLOB和NCLOB的.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/195110/viewspace-680474/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於Oracle的BLOB和CLOBOracle
- 7.82 EMPTY_BLOB, EMPTY_CLOB
- Java 中 CLOB 和字串之間的轉換Java字串
- Mysql BLOB、BLOB與TEXT區別及效能影響、將BLOB型別轉換成VARCHAR型別MySql型別
- JDBC 處理CLob和Blob型別資料JDBC型別
- Blob實現與File DataURL canvas相互轉換示例詳解Canvas
- 如何檢視ORACLE的LOB(BLOB和CLOB)物件佔用的大小Oracle物件
- javascript 將URL轉換為Blob、base64JavaScript
- 初探JavaScript PDF blob轉換為Word docx方法JavaScript
- 前端圖片canvas,file,blob,DataURL等格式轉換前端Canvas
- oid轉blob問題
- Oracle blob型別資料轉換成 base64編碼Oracle型別
- RGB與YIQ的轉換
- 程式的狀態與轉換
- list與字串轉換字串
- 如何更換Azure SQL DB blob storage credentialsSQL
- Torrent檔案的解析與轉換
- QString與QByteArray互相轉換的方法
- [20180416]clob的插入.txt
- 轉換流與列印流
- UIImage與Iplimage相互轉換UI
- SDOM與QDOM相互轉換
- float與byte[]互相轉換
- DataTable與List相互轉換
- 旋轉矩陣與尤拉角的相互轉換矩陣
- Spark SQL中的RDD與DataFrame轉換SparkSQL
- python str與bytes之間的轉換Python
- jenkins:實現Jenkinsfile與Json的轉換JenkinsJSON
- xml與陣列的相互轉換——phpXML陣列PHP
- SpringMVC的亂碼與時間轉換SpringMVC
- JavaWeb——JDBC八股文、JSBC使用儲存過程、儲存函式、處理CLOB/BLOB型別JavaWebJDBCJS儲存過程儲存函式型別
- pyhanlp 繁簡轉換之拼音轉換與字元正則化HanLP字元
- 【Go】IP地址轉換:數字與字串之間高效轉換Go字串
- Hive日期、時間轉換:YYYY-MM-DD與YYYYMMDD;hh.mm.ss與hhmmss的相互轉換HiveHMM
- iOS引用轉換:Foundation與Core Foundation物件互相轉換(__CFString轉NSString,void *轉id等等)iOS物件
- 陣列與字串方法與相互轉換陣列字串
- 1、交換與定址轉發
- JSON字串與HashMap相互轉換JSON字串HashMap
- java 物件與xml相互轉換Java物件XML