How to return temporary CLOB instance from stored function in Pl/SQL?

最后都变了- 提交于 2019-12-05 17:07:23

In a comment you said:

clob.getSubString(0, clob.length()) throws: java.sql.SQLException: Invalid argument(s) in call at oracle.sql.CLOB.getSubString(CLOB.java:236) while clob.length() returns actual length of my clob

The documentation of getSubString states that:

pos - the first character of the substring to be extracted. The first character is at position 1.

With a simple function to generate and return a CLOB, I can retrieve it over JDBC (ojdbc5 or ojdbc6) with no problems, either with getCLOB() or getString(). But if I try to assign the Oracle.sql.CLOB retrieved with getCLOB to a String using

String x = getSubString(0, clob.length());

then I also get the Invalid argument(s) in call error. Just changing that to:

String x = getSubString(1, clob.length());

works. So it seems to have nothing to do with the temporary allocation in the function, or the CLOB size. I don't understand why you didn't have a problem with smaller CLOBs - maybe your logic just didn't hit this if they were small?

In the meantime you've worked around this with clob.getCharacterStream().read(), so this may be a bit irrelevant now.

I created the function to return a clob, with random generated data, lenght is 200k characters.

create function f_clob
return clob is
   l_clob      CLOB := EMPTY_CLOB;
   l_len       BINARY_INTEGER;
   l_content   VARCHAR2(32000);
BEGIN
   dbms_lob.createtemporary(l_clob, TRUE);
   dbms_lob.open(l_clob, dbms_lob.lob_readwrite);
   --
   for i in 1..100
   loop
      l_content := dbms_random.string('A', 2000);
      l_len := length(l_content);
      dbms_lob.writeappend(l_clob, l_len, l_content);
   end loop;
   dbms_lob.close(l_clob);
   --
   return l_clob;
end f_clob;

Then I call the function:

select to_char(substr(f_clob, 1, 200)) clob_chunk
from  (
   select 1
   from dual
   union
   select 2
   from dual)

And I always get data out as a result. I wonder why your function isn't returning data.

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