将表名作为参数传递时PL / SQL函数不起作用

时间:2014-06-10 08:54:39

标签: function plsql oracle11g

以下是我的功能,

create or replace 
FUNCTION checkXML
 (idx in number , tblname in varchar2) 
 return xmltype 
is 
required_xml XMLTYPE;
saved_hash_value raw(50);
current_hash_value raw(50);
xml_not_equal EXCEPTION;

begin
execute immediate 'select checkfield , my_hash(extract(xmlcol,'/')) , xmlcol into saved_hash_value , 
                   current_hash_value , required_xml  from ' || tblname || '  where indexid =' || idx ;


if saved_hash_value = current_hash_value then
  return required_xml;
else
  RAISE xml_not_equal;  
end if;

end;

我想知道哪里出错了。

收到的错误消息是,

ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "SYSTEM.CHECKXML", line 11
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    
*Action:

1 个答案:

答案 0 :(得分:3)

您的SQL语句中有未转义的单引号,因此斜杠/被解释为除法符号,而不是字符串的一部分。你需要添加转义符:

my_hash(extract(xmlcol, ''/''))

您还应该为idx使用绑定变量,并且您的into位于动态SQL的错误位置:

execute immediate 'select checkfield , my_hash(extract(xmlcol, ''/'')) ,
  xmlcol from ' || tblname || ' where indexed = :idx'
into saved_hash_value , current_hash_value , required_xml
using idx;

也不确定你试图通过例外实现的目标。你已经在本地声明它然后尝试提升它,但我认为这只会产生一个未处理的用户定义的异常错误。您可能只想raise an application error使用自己的错误编号和消息,例如:

  ...
  if saved_hash_value != current_hash_value then
    raise_application_error(-20001, 'XML hash is not correct');  
  end if;
  return required_xml;
end;
相关问题