如何在Oracle存储过程中将CLOB数据类型作为输出参数返回?

时间:2017-04-05 21:07:28

标签: xml oracle stored-procedures clob

我想从oracle存储过程返回CLOB数据类型(XML数据)的输出参数。

我的程序看起来像这样

create or replace PROCEDURE myProcedure
(
  myParam1 IN NUMBER 
 ,myParam2 IN NUMBER 
 ,myParam3 OUT CLOB 
) AS  
   xml_bits CLOB;
BEGIN

 select  xmldataColumn into xml_bits 
 from myTable WHERE id = myParam1 AND SESSION_ID = myParam2;

  IMPCPM_XML := xml_bits;
END myProcedure;

我编译了这个,当我尝试执行时,它给了我以下错误。

exact fetch returns more than requested number of rows

如何将大型xmldata作为out param返回?

提前致谢。

1 个答案:

答案 0 :(得分:1)

该错误与返回CLOB值无关。这是查询:

select  xmldataColumn into xml_bits 
from myTable WHERE id = myParam1 AND SESSION_ID = myParam2;

返回多行,并使用SELECT ... INTO ...要求查询只返回一行。

您可以为ROWNUM = 1添加过滤器:

create or replace PROCEDURE myProcedure
(
  myParam1 IN NUMBER 
 ,myParam2 IN NUMBER 
 ,myParam3 OUT CLOB 
) AS  xml_bits CLOB;
BEGIN
  select xmldataColumn
  into   xml_bits 
  from   myTable
  WHERE  id = myParam1
  AND    SESSION_ID = myParam2
  AND    ROWNUM = 1;
END myProcedure;
/

或者您可以处理异常:

create or replace PROCEDURE myProcedure
(
  myParam1 IN NUMBER 
 ,myParam2 IN NUMBER 
 ,myParam3 OUT CLOB 
) AS  xml_bits CLOB;
BEGIN
  select xmldataColumn
  into   xml_bits 
  from   myTable
  WHERE  id = myParam1
  AND    SESSION_ID = myParam2;
EXCEPTION
  WHEN TOO_MANY_ROWS THEN
    xml_bits := NULL;
END myProcedure;
/