2013-12-10 109 views
3

我創建下面的函數dbms_lob.append錯誤還是我做錯了

function lob_replace(p_lob in out clob,p_what in varchar2,p_with in clob) return clob 
    as 
     l_temp_number number; 
     l_temp_number_1 number; 
     l_temp_clob clob; 
     l_return_clob clob; 
     l_temp1_clob clob; 
     l_temp2_clob clob; 
    begin 
       l_temp_number:=dbms_lob.instr(p_lob, p_what); 
       --Create a lob locator 
       DBMS_LOB.createtemporary(l_temp_clob,true); 
       DBMS_LOB.createtemporary(l_temp1_clob,true); 
       DBMS_LOB.createtemporary(l_temp2_clob,true); 

       ---substract and build the LOBs 
       l_temp_number_1:=length(p_lob); 
       l_temp_clob:=dbms_lob.substr(p_lob,l_temp_number-1,1); 
       l_temp1_clob:=dbms_lob.substr(p_lob,l_temp_number_1-l_temp_number+1 ,l_temp_number +length(p_what)); 

       --append three diff lob to one 
       dbms_lob.append(l_temp2_clob,l_temp_clob); 
       dbms_lob.append(l_temp2_clob,p_with); 
       dbms_lob.append(l_temp2_clob,l_temp1_clob); 

       l_return_clob :=l_temp2_clob; 

       --remove the tmp lob 
       DBMS_LOB.freetemporary(l_temp_clob); 
       DBMS_LOB.freetemporary(l_temp1_clob); 
       DBMS_LOB.freetemporary(l_temp2_clob); 

       return l_return_clob; 
    end; 

如果我調用該函數如下

declare 
    temp clob; 
begin 
    temp:='replace this #a#'; 
    temp:=lob_replace(temp,'#a#','with this'); 
end; 

將拋出錯誤它會拋出以下錯誤

ORA-06502: PL/SQL: numeric or value error: invalid LOB locator specified: ORA-22275 
    ORA-06512: at "SYS.DBMS_LOB", line 639 
    ORA-06512: at "LOB_REPLACE", line 24 
    ORA-06512: at line 5 

但是這個wi會不會引發錯誤

declare 
    temp clob; 
begin 
    temp:='replace this #a# '; 
    temp:=lob_replace(temp,'#a#','with this'); 
end; 

請注意最後的temp:='replace this #a# ';

額外的空間是否有任何人知道這樣做的原因?

回答

0

當您構建l_temp1_clob時,如果有任何要添加的內容,您只能添加剩下的高球。改變你的代碼:

l_temp1_clob:=dbms_lob.substr(p_lob,l_temp_number_1-l_temp_number+1 ,l_temp_number +length(p_what)); 

要這樣:

if l_temp_number +length(p_what) < l_temp_number_1 
then 
    l_temp1_clob := dbms_lob.substr(p_lob, l_temp_number_1 - l_temp_number - 1 , l_temp_number +length(p_what)); 
end if; 

的邏輯是:如果LOB SUBSTR開始是高球結束前。

此外,dd變量是不是在你的測試呼叫聲明:

declare 
    temp clob; 
begin 
    temp:='replace this #a#'; 
    temp:=lob_replace(dd,'#a#','with this'); 
end; 
相關問題