oracle 中止 PL/SQL 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/891458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:52:41  来源:igfitidea点击:

Abort a PL/SQL program

oracleplsql

提问by Margaret

How do I get a PL/SQL program to end halfway through? I haven't been able to find any way to gracefully end the program if an exception occurs - if I handle it, it loops back into the code.

如何让 PL/SQL 程序中途结束?如果发生异常,我无法找到任何方式优雅地结束程序 - 如果我处理它,它会循环回到代码中。

Basically what I want to do is force the app not to run in certain conditions. So, I want to add something like this to the top of the program:

基本上我想做的是强制应用程序不在某些条件下运行。所以,我想在程序的顶部添加这样的东西:

BEGIN
    IF [condition]
        EXIT
    END IF
    [the rest of the program]
END

The suggested way is to throw an exception, but the block may well be an inner block - so the program outside of the block just keeps going.

建议的方法是抛出异常,但该块很可能是一个内部块 - 因此该块外的程序会继续运行。

回答by Matthew Watson

You can use RETURN

您可以使用返回

MWATSON@> set serveroutput on
MWATSON@> !cat test.sql

BEGIN
 IF 1 = 1 THEN
    DBMS_OUTPUT.PUT_LINE('ABOUT TO EXIT');
    RETURN;
  END IF;
  DBMS_OUTPUT.PUT_LINE('DID NOT EXIT');
END;

MWATSON@> @test
  8  /
ABOUT TO EXIT

PL/SQL procedure successfully completed.

MWATSON@> 

回答by KLeonine

I know it's too late to respond, but I have one more way that is not mentioned in the previous answers.

我知道现在做出回应为时已晚,但我还有一种以前的答案中没有提到的方法。

Use RAISE_APPLICATION_ERROR and catch this exception in EXCEPTION section. As this rolls back uncommitted transactions, make sure to commit them explicitly if required.

使用 RAISE_APPLICATION_ERROR 并在 EXCEPTION 部分捕获此异常。由于这会回滚未提交的事务,因此请确保在需要时明确提交它们。

This way you can gracefully return from the program, instead of doing exception handling in IF block when you use RETURN.

这样你就可以从程序中优雅地返回,而不是在使用 RETURN 时在 IF 块中进行异常处理。

I used this for reference. http://www.plsql-tutorial.com/plsql-exception-handling.htm

我用这个作为参考。 http://www.plsql-tutorial.com/plsql-exception-handling.htm

回答by Jeffrey Kemp

If you raise an exception that the block does not handle, the exception is always raised to the caller. So the easiest way to stop processing is raise an exception that is not handled anywhere in the call stack.

如果您引发块未处理的异常,则始终将异常引发给调用者。因此,停止处理的最简单方法是引发一个在调用堆栈中任何地方都未处理的异常。

e.g.

例如

DECLARE
    e_halt_processing EXCEPTION;
BEGIN
    IF [condition] THEN
        RAISE e_halt_processing;
    END IF;
    [the rest of the program]
END;

回答by victor hugo

I don't know PL/SQL but why don't you try (using your words):

我不知道 PL/SQL 但你为什么不试试(用你的话):

BEGIN
    IF [!condition]
        [the rest of the program]
    END IF
END

Just thinking

只是想

回答by Mac

As long as you use sequential (not nested) pl/sql blocks and separate exception handling then RAISE works perfectly well. If you are RAISE ing exeptions in nested blocks then beware of a race condition.

只要您使用顺序(非嵌套)pl/sql 块和单独的异常处理,那么 RAISE 就可以很好地工作。如果您在嵌套块中 RAISE ing exeptions,请注意竞争条件。