在Execute Immediate中传递XML

时间:2016-04-07 10:56:37

标签: oracle11g

      varSQL: = 'DECLARE


      varOptionId NUMBER;
      varXML XMLTYPE;
      varHsCode VARCHAR2(200);
      varHsCodeCount NUMBER;
      varId NUMBER;
      RVAL  OBJ_RETURN:=OBJ_RETURN(NULL,NULL,NULL,NULL);

      BEGIN

      varXML:=:refXML;


      varHsCode:= varXML.EXTRACT('/OBJECT/HsCodes/HsCode/text()').GETSTRINGVAL();


      :out:=varHsCode;

       END';

       EXECUTE IMMEDIATE varSQL USING PXML,OUT varOut;

这会出现以下错误:

ORA-06550:第9行,第15栏: PLS-00382:表达式类型错误 ORA-06550:第9行,第2栏: PL / SQL:语句被忽略 ORA-06512:at" BIZZXE_V2_SCH.SYSTEM_UTILITY",第225行 06550. 00000 - "行%s,列%s:\ n%s" *原因:通常是PL / SQL编译错误。 *操作:

是否可以传递XML?

1 个答案:

答案 0 :(得分:0)

是的,您可以将XMLType变量作为绑定参数传递:

declare
    varsql varchar2(1024);
    varout varchar2(4);
    pxml xmltype := xmltype('<OBJECT><HsCodes><HsCode>Test</HsCode></HsCodes></OBJECT>');
begin
    varSQL := 'DECLARE
 varOptionId NUMBER;
 varXML XMLTYPE;
 varHsCode VARCHAR2(200);
 varHsCodeCount NUMBER;
 varId NUMBER;
 --RVAL  OBJ_RETURN:=OBJ_RETURN(NULL,NULL,NULL,NULL);
 BEGIN
 varXML:=:refXML;

 varHsCode:= varXML.EXTRACT(''/OBJECT/HsCodes/HsCode/text()'').GETSTRINGVAL();

 :out:=varHsCode;
END;';

    EXECUTE IMMEDIATE varSQL USING PXML,OUT varOut;
    dbms_output.put_line(varOut);
end;
/

PL/SQL procedure successfully completed.

Test

您获得的错误是因为您已将PXML声明为字符串; varchar2CLOB变量:

declare
    varsql varchar2(1024);
    varout varchar2(4);
</HsCodes></OBJECT>');
    pxml varchar2(64) := '<OBJECT><HsCodes><HsCode>Test</HsCode></HsCodes></OBJECT>';
begin
    varSQL := 'DECLARE
 varOptionId NUMBER;
 varXML XMLTYPE;
 varHsCode VARCHAR2(200);
 varHsCodeCount NUMBER;
 varId NUMBER;
 --RVAL  OBJ_RETURN:=OBJ_RETURN(NULL,NULL,NULL,NULL);
 BEGIN
 varXML:=:refXML;

 varHsCode:= varXML.EXTRACT(''/OBJECT/HsCodes/HsCode/text()'').GETSTRINGVAL();

 :out:=varHsCode;
END;';

    EXECUTE IMMEDIATE varSQL USING PXML,OUT varOut;
    dbms_output.put_line(varOut);
end;
/

Error report -
ORA-06550: line 9, column 10:
PLS-00382: expression is of wrong type
ORA-06550: line 9, column 2:
PL/SQL: Statement ignored
ORA-06512: at line 21

在您的示例中,无论如何都没有理由使用动态SQL,但也许您的真实示例更复杂。