简体   繁体   English

oracle plsql中的动态goto

[英]dynamic goto in oracle plsql

I have a procedure that updates ~700 rows from a table based on certain dates. 我有一个程序可以根据某些日期从表中更新约700行。 I want, if the procedure crashes from some reason to restart the procedure from where it left, not from the beginтing ( ex: go to row214). 我想要,如果该程序由于某种原因而崩溃,请从它离开的地方而不是从开头开始重新启动该程序(例如:转到row214)。

If I try something like this: 如果我尝试这样的事情:

begin

goto &label;

<N1>;

dbms_output.put_line(1);

<N2>;

dbms_output.put_line(2);

end;

it works, but that will presume human interaction, and I would want the procedure to do this automatically. 它可以工作,但是这将假定人与人之间的互动,我希望该过程可以自动执行此操作。

How can I do something like this without the ampersand(&)? 没有&符,我该怎么做? If I declare the label as a variable, it doesn't work, or I don't know how to make it work. 如果我将标签声明为变量,则它不起作用,或者我不知道如何使它起作用。

You cannot do a "dynamic GOTO" in a static PL/SQL block. 您不能在静态PL / SQL块中执行“动态GOTO”。 The reason is that any argument that follows the GOTO keyword will be interpreted as the label you want to jump to, not as an expression that will evaluate to the label. 原因是, GOTO关键字GOTO任何参数都将被解释为您要跳转到的标签,而不是会计算为标签的表达式。

About the closest you'll get is this: 关于您得到的最接近的信息是:

declare 
  l_label VARCHAR2(30) := 'N2';
BEGIN

 CASE l_label
   WHEN 'N1' THEN GOTO N1;
   WHEN 'N2' THEN GOTO N2;
   ELSE raise_application_error (-20001, 'Unknown label: ' || l_label);
 END CASE;

 <<N1>>
  DBMS_OUTPUT.put_line (1);

 <<N2>>
  DBMS_OUTPUT.put_line (2);
END;

While you cannot do this in static PL/SQL, your use of ampersand (&) implies that you might be using SQL*Plus and merely want to avoid having a human have to type in the value of the label at runtime. 虽然您无法在静态PL / SQL中执行此操作,但是使用&符表示您可能正在使用SQL * Plus,而只是想避免让人类不得不在运行时键入标签的值。

In SQL*Plus, you can do this. 在SQL * Plus中,您可以执行此操作。

column label_value old_value label;
set termout off
REM Replace the following SQL with something that will get the correct label.
select 'N2' label_value FROM dual;
set termout on
set verify off

set serveroutput on

begin
  goto &label;
  <<N1>>
  dbms_output.put_line('N1');
  <<N2>>
  dbms_output.put_line('N2');
end;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM