PLSQL - 获取发生异常的行

时间:2013-12-23 09:56:16

标签: sql xml oracle plsql

是否可以捕获发生异常的行?

给出以下PL / SQL块:

l_path1_value := l_xmltype.extract('/SOME_PATH1').getStringVal();
l_path2_value := l_xmltype.extract('/SOME_PATH2').getStringVal();
l_path3_value := l_xmltype.extract('/SOME_PATH3').getStringVal();

extract函数返回null时,将抛出SELF_IS_NULL异常。抛出错误时是否可以获取错误行?

WHEN SELF_IS_NULL THEN
    --get error line and save it somewhere

2 个答案:

答案 0 :(得分:0)

尝试为extract函数创建一个包装函数,并定义一个EXCEPTION SELF_IS_NULL并在它返回NULL时使用RAISE SELF_IS_NULL;

dbms_output.put_line( SQLERRM||chr(10)||dbms_utility.format_error_backtrace );

请把它放在你的例外程序块中。它从它的起源位置打印堆栈跟踪(行号)

答案 1 :(得分:0)

实现此目的的一种方法是使用检查点变量,类似于以下内容:

BEGIN
  strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/QUANTITY/VALUE/text()';
  l_XMLvalue := l_xmltype.extract(strXMLelement ).getStringVal();
  dbms_output.put_line('QUANTITY: '||l_XMLvalue);

  strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/DOCUMENTID/text()';
  l_XMLdocument_id := l_xmltype.extract(strXMLelement ).getStringVal();
  dbms_output.put_line('DOCUMENT ID:' ||l_XMLdocument_id);

  strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/USERAREA/DOCUMENTRELEASE/text()';
  l_XMLdoc_release := l_xmltype.extract(strXMLelement).getStringVal();
  dbms_output.put_line('DOCUMENT RELEASE:' ||l_XMLdoc_release);

  strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/LINENUM/text()';
  l_XMLlinenum := l_xmltype.extract(strXMLelement).getStringVal();
  dbms_output.put_line('LINE NUMBER:' ||l_XMLlinenum);

  strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/SCHLINENUM/text()';
  l_XMLsch_lnum := l_xmltype.extract(strXMLelement).getStringVal();
  dbms_output.put_line('SCHIPMENT LINE NUMBER:' ||l_XMLsch_lnum);
EXCEPTION
  WHEN SELF_IS_NULL THEN
    DBMS_OUTPUT.PUT_LINE('Error extracting ' || strXMLelement || ' : ' ||
                         'SQLCODE=' || SQLCODE || '  SQLERRM=' || SQLERRM);
END;

分享并享受。