Oracle DBMS_LOB: Using DBMS_LOB.convertToBlob multiple times in PLSQL block

心已入冬 提交于 2019-12-23 01:29:45

问题


When I try to use DBMS_LOB.convertToBlob procedure multiple times in the same PLSQL block only the first execution works as expected. All additional executions produce empty blobs. Here is an example:

DECLARE 

v_temp1 varchar2(32767) := 'absd'; 
V_temp1_blob BLOB; 
V_temp2 varchar2(32767) := 'efghi'; 
V_temp2_blob BLOB;

v_in      integer := 1;
v_out     integer := 1;
v_lang    integer := 0;
v_warning integer := 0;

BEGIN

DBMS_LOB.createTemporary (V_temp1_blob, TRUE);  
DBMS_LOB.convertToBlob(V_temp1_blob, V_temp1, DBMS_LOB.LOBMAXSIZE, v_in, v_out, DBMS_LOB.DEFAULT_CSID, v_lang, v_warning);
dbms_output.put_line('V_temp1_blob: ' || dbms_lob.getlength(V_temp1_blob));

DBMS_LOB.createTemporary (V_temp2_blob, TRUE);
DBMS_LOB.convertToBlob(V_temp2_blob, V_temp2, DBMS_LOB.LOBMAXSIZE, v_in, v_out, DBMS_LOB.DEFAULT_CSID, v_lang, v_warning);
dbms_output.put_line('V_temp2_blob: ' || dbms_lob.getlength(V_temp2_blob));

END;

OUTPUT:

V_temp1_blob: 4
V_temp2_blob: 0

My expected output is:

V_temp1_blob: 4
V_temp2_blob: 5

What am I missing here ?


回答1:


Your v_in and v_out local variables are being passed in as the dest_offset and src_offset parameters of the convertToBlob procedure. These are in out parameters so they are modified by the call.

dest_offset, which is what you're passing v_in for, is defined to be

(IN)Offset in bytes in the destination LOB for the start of the write. Specify a value of 1 to start at the beginning of the LOB.

(OUT)The new offset in bytes after the end of the write.

while src_offset, which is what you're passing v_out for, is defined to be

(IN)Offset in characters in the source LOB for the start of the read.

(OUT)Offset in characters in the source LOB right after the end of the read.

For the first call, you're passing in 1 in both cases. For the second call, you're passing in the offsets from the first LOB. You'd need to re-initialize both parameters to 1 before making the second call.

I'd also suggest that you probably want to choose better variable names than v_in and v_out for these variables. If they were named v_src_offset and v_dest_offset, that would generally make your code clearer and might make the bug easier to spot.



来源:https://stackoverflow.com/questions/31902029/oracle-dbms-lob-using-dbms-lob-converttoblob-multiple-times-in-plsql-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!