Function 将表名作为参数传递时PL/SQL函数不工作

Function 将表名作为参数传递时PL/SQL函数不工作,function,plsql,oracle11g,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

以下是我的职能

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:

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

my_hash(extract(xmlcol, ''/''))
您还应该为
idx
使用bind变量,并且您的
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;
除了这个例外,我也不确定你想要实现什么。您已经在本地声明了它,然后尝试引发它,但我认为这只会生成一个未处理的用户定义异常错误。您可能只想使用自己的错误号和消息,例如:

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