Đây là một đoạn mã hoạt động. Tôi khuyên bạn nên sử dụng các con trỏ rõ ràng thay vì các con trỏ ngầm (CHO tôi VÀO (chọn ...)), cho mục đích hiệu suất.
Đầu tiên, đây là tập lệnh để tạo testcase.
create table test (c clob);
insert into test (c) values (
'azertyuiop
qsdfghjklm
wxcvbn
');
Sau đó, đây là tập lệnh để đọc từng dòng Clob:
/* Formatted on 28/08/2012 14:16:52 (QP5 v5.115.810.9015) */
declare
nStartIndex number := 1;
nEndIndex number := 1;
nLineIndex number := 0;
vLine varchar2(2000);
cursor c_clob is
select c from test;
c clob;
-------------------------------
procedure printout
(p_clob in out nocopy clob) is
offset number := 1;
amount number := 32767;
len number := dbms_lob.getlength(p_clob);
lc_buffer varchar2(32767);
i pls_integer := 1;
begin
if ( dbms_lob.isopen(p_clob) != 1 ) then
dbms_lob.open(p_clob, 0);
end if;
amount := instr(p_clob, chr(10), offset);
while ( offset < len )
loop
dbms_lob.read(p_clob, amount, offset, lc_buffer);
dbms_output.put_line('Line #'||i||':'||lc_buffer);
offset := offset + amount;
i := i + 1;
end loop;
if ( dbms_lob.isopen(p_clob) = 1 ) then
dbms_lob.close(p_clob);
end if;
exception
when others then
dbms_output.put_line('Error : '||sqlerrm);
end printout;
---------------------------
begin
dbms_output.put_line('-----------');
open c_clob;
loop
fetch c_clob into c;
exit when c_clob%notfound;
printout(c);
end loop;
close c_clob;
end;
biến 'số tiền' được sử dụng để phát hiện vị trí cuối dòng. Hãy điền đầy đủ, trong một số trường hợp, cuối dòng là CHR (10) || CHR (13) (CR + LF), và trong một số trường hợp khác, nó chỉ là CHR (10).