天天看點

腳本插入CLOB字段

dbms_lob.writeappend(v_clob, length(v_buffer), v_buffer);

declare

v_buffer clob;

v_clob clob;

begin

insert into t1 (id,cl) values (2, empty_clob())

returning cl into v_clob;

v_buffer := dbms_random.string('A',4000)||dbms_random.string('A',4000);

dbms_lob.writeappend(v_clob, length(v_buffer), v_buffer);

commit;

end;

另一例子:

BFILE包含二進制資料,是以當通過BFILE方式加載資料到CLOB/NCLOB時,Oracle不進行字元集轉化。

如果字元集是可變長的,例如UTF-8或ZHS16GBK,Oracle使用UCS2存儲LOB資料。是以如果想BFILE檔案資料能夠正确

加載入資料庫,那麼檔案需要以USC2字元集存儲。

那麼還有一點需要注意的是,如果以Unicode模式存儲檔案,那麼檔案會增加兩個位元組:

'FF FE'.

在加載檔案時我們需要跳過這兩個位元組。

在Windows上我們存儲檔案時選擇Unicode方式,我輸入的字元為:

test測試

Oracle BFile資料加載問題

研究

然後我們可以加載這個檔案的内容,注意比較前後兩個過程不同之處:

SQL> create table t (

2 name varchar2(30),

3 content clob

4 )

5 /

Table created.

SQL>

SQL> create or replace directory dir1 as '/opt/oracle';

Directory created.

SQL> declare

2 l_bfile bfile;

3 l_clob clob;

4 l_str varchar2(1000);

5 begin

6 insert into t (name,content)

7 values ('b.txt',empty_clob())

8 returning content into l_clob;

9

10 l_bfile := bfilename('DIR1','b.txt');

11

12 dbms_lob.fileopen(l_bfile);

13 dbms_lob.loadfromfile(l_clob,l_bfile,dbms_lob.getlength(l_bfile));

14 dbms_lob.fileclose(l_bfile);

15 commit;

16 exception when others then

17 l_str:=sqlerrm(sqlcode);

18 dbms_output.put_line(l_str);

19 end;

20 /

PL/SQL procedure successfully completed.

SQL> select * from t;

NAME CONTENT

------------------------------ ----------------------------------------------

b.txt ?test測試

13 dbms_lob.loadfromfile(l_clob,l_bfile,dbms_lob.getlength(l_bfile)-2,

14 src_offset => 3);

15 dbms_lob.fileclose(l_bfile);

16 commit;

17 exception when others then

18 l_str:=sqlerrm(sqlcode);

19 dbms_output.put_line(l_str);