CLOB裏存的是2進制
判定長度 DBMS_LOB.GETLENGTH(col1)
獲取文本 DBMS_LOB.SUBSTR(col1,n,pos)
DBMS_LOB.SUBSTR(col1,10,1)表示從第1個字節開始取出10個字節
DBMS_LOB.SUBSTR(CLOB_VAR,32767)表示截取CLOB變量保存的全部數據
DBMS_LOB.FILECLOSE(IMG_BFILE)關閉文件
clob轉化爲字符串,SELECT UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(col1,10,1)) FROM tab1;
如果clob中包含空格則不能使用。
將clob類型轉化成字符串
create or replace function getclob(
table_name in varchar2,
field_id in varchar2,
field_name in varchar2,
v_id in number,
v_pos in number) return varchar2
is
lobloc clob;
buffer varchar2(32767);
amount number := 2000;
offset number := 1;
query_str varchar2(1000);
begin
query_str :='select '||field_name||' from '||table_name||' where '||field_id||'= :id ';
EXECUTE IMMEDIATE query_str INTO lobloc USING v_id;
offset:=offset+(v_pos-1)*2000;
dbms_lob.read(lobloc,amount,offset,buffer);
return buffer;
exception
when no_data_found then
return buffer;
end;
兩個寫數據的存儲過程
dbms_lob.append 和 dbms_lob.write
append存儲過程用於將一個大對象添加到另一個大對象中,此時是將源大對象的內容全部添加過去。append存儲過程的語法如下:
dbms_lob.append(
dest_lob in out nocopyblob,
src_lob in blob);
dbms_lob.append(
dest_lob in out nocopy clobcharacter set any_cs,
src_lob in clob character setdest_lob%charset);
其中,各個參數的含義如下:
dest_lob是被源lob添加到的目標lob的定位器
src_lob是源lob的定位器
any_cs用來指定字符集。
write存儲過程
write存儲過程能夠將數據寫入大型對象中。寫的位置是從大型對象開始處的某個絕對偏移地址,數據從緩衝區參數被寫入。寫操作將覆蓋已經在大型對象偏移地址處存在的任何長度爲指定的數據。如果輸入數多於在緩衝區的數據,將產生一個錯誤。如果輸入數量小於在緩衝區的數據,那麼只有緩衝區的數據字節活字符被寫給大型對象。
write存儲過程的語法如下:
dbms_lob.write(
lob_loc in out nocopy blob,
amount in binary_integer,
offset in integer,
buffer in raw);
dbms_lob.write(
lob_loc in out nocopy clob character setany_cs,
amount in binary_integer,
offset in integer,
buffer in varchar2 character setlob_loc%charset);
其中各個參數的含義如下:
lob_loc是要操作的大型對象定位器。
amount是要寫道大型對象中去的字節數量。
offset是指定將數據寫入到大型對象什麼位置的偏移地址。
buffer是寫入到大型對象的數據緩衝區。
any_cs指定要使用的字符集。
示例:
declare
source_lob clob;
dest_lob clob;
write_amountinteger:=18;
writing_positioninteger;
buffer varchar2(20) := 'Addedtext to clob';
begin
select clob_locator intodest_lob from mylobs where lob_index = 4 for update;
select clob_locator intosource_lob from mylobs where lob_index = 1;
dbms_lob.append(dest_lob,source_lob);
commit;
select clob_locator intodest_lob from mylobs where lob_index = 5 for update;
writing_position :=dbms_lob.getlength(dest_lob) + 1;
dbms_lob.write(dest_lob,write_amount, writing_position, buffer);
commit;
end;
/
SQL> select * frommylobs;
LOB_INDEX---CLOB_LOCATOR-------------------------------------------------------
1 I Love Oracle 9i
2 Oracle 9i is powerful
3 Oracle 9i is the most popular database in the world
4 I Love Oracle 9iI Love Oracle 9i
5 Oracle 9i is powerfulAdded text to clob
判定長度 DBMS_LOB.GETLENGTH(col1)
獲取文本 DBMS_LOB.SUBSTR(col1,n,pos)
DBMS_LOB.SUBSTR(col1,10,1)表示從第1個字節開始取出10個字節
DBMS_LOB.SUBSTR(CLOB_VAR,32767)表示截取CLOB變量保存的全部數據
DBMS_LOB.FILECLOSE(IMG_BFILE)關閉文件
clob轉化爲字符串,SELECT UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(col1,10,1)) FROM tab1;
如果clob中包含空格則不能使用。
將clob類型轉化成字符串
create or replace function getclob(
table_name in varchar2,
field_id in varchar2,
field_name in varchar2,
v_id in number,
v_pos in number) return varchar2
is
lobloc clob;
buffer varchar2(32767);
amount number := 2000;
offset number := 1;
query_str varchar2(1000);
begin
query_str :='select '||field_name||' from '||table_name||' where '||field_id||'= :id ';
EXECUTE IMMEDIATE query_str INTO lobloc USING v_id;
offset:=offset+(v_pos-1)*2000;
dbms_lob.read(lobloc,amount,offset,buffer);
return buffer;
exception
when no_data_found then
return buffer;
end;
兩個寫數據的存儲過程
dbms_lob.append 和 dbms_lob.write
append存儲過程用於將一個大對象添加到另一個大對象中,此時是將源大對象的內容全部添加過去。append存儲過程的語法如下:
dbms_lob.append(
dest_lob in out nocopyblob,
src_lob in blob);
dbms_lob.append(
dest_lob in out nocopy clobcharacter set any_cs,
src_lob in clob character setdest_lob%charset);
其中,各個參數的含義如下:
dest_lob是被源lob添加到的目標lob的定位器
src_lob是源lob的定位器
any_cs用來指定字符集。
write存儲過程
write存儲過程能夠將數據寫入大型對象中。寫的位置是從大型對象開始處的某個絕對偏移地址,數據從緩衝區參數被寫入。寫操作將覆蓋已經在大型對象偏移地址處存在的任何長度爲指定的數據。如果輸入數多於在緩衝區的數據,將產生一個錯誤。如果輸入數量小於在緩衝區的數據,那麼只有緩衝區的數據字節活字符被寫給大型對象。
write存儲過程的語法如下:
dbms_lob.write(
lob_loc in out nocopy blob,
amount in binary_integer,
offset in integer,
buffer in raw);
dbms_lob.write(
lob_loc in out nocopy clob character setany_cs,
amount in binary_integer,
offset in integer,
buffer in varchar2 character setlob_loc%charset);
其中各個參數的含義如下:
lob_loc是要操作的大型對象定位器。
amount是要寫道大型對象中去的字節數量。
offset是指定將數據寫入到大型對象什麼位置的偏移地址。
buffer是寫入到大型對象的數據緩衝區。
any_cs指定要使用的字符集。
示例:
declare
source_lob clob;
dest_lob clob;
write_amountinteger:=18;
writing_positioninteger;
buffer varchar2(20) := 'Addedtext to clob';
begin
select clob_locator intodest_lob from mylobs where lob_index = 4 for update;
select clob_locator intosource_lob from mylobs where lob_index = 1;
dbms_lob.append(dest_lob,source_lob);
commit;
select clob_locator intodest_lob from mylobs where lob_index = 5 for update;
writing_position :=dbms_lob.getlength(dest_lob) + 1;
dbms_lob.write(dest_lob,write_amount, writing_position, buffer);
commit;
end;
/
SQL> select * frommylobs;
LOB_INDEX---CLOB_LOCATOR-------------------------------------------------------
1 I Love Oracle 9i
2 Oracle 9i is powerful
3 Oracle 9i is the most popular database in the world
4 I Love Oracle 9iI Love Oracle 9i
5 Oracle 9i is powerfulAdded text to clob