Exception PL/SQL



小心我的英语很糟糕。

西班牙论坛上没有人回答。

疑虑:

  1. ?如何控制空条目的异常?(解释如下(
  2. ?如果有空条目,变量中会保存什么值

我创建了这个变量:

DECLARE
v_input NUMBER(8) := &entry;

我没有给出数值,我的意思是在弹出窗口中,我直接接受,没有写任何东西。

弹出IMG

当我读到错误代码(我将在最后留下(时,我看到了这一行。

PLS-00103:遇到符号";"当期望出现以下情况时:

所以我想,如果你做一个空条目,变量的默认值是->

然后我想控制异常,创建一个IF,如果将我的变量(带有空条目/值(与";"进行比较你可以看到下面。

BEGIN
DBMS_OUTPUT.PUT_LINE(v_codigo);
IF(v_codigo = ';')THEN
RAISE v_excepcion;
END IF;
EXCEPTION
when v_excepcion then 
DBMS_OUTPUT.PUTLINE('No has insertado un valor válido');
END;

错误代码

Informe de error -
ORA-06550: línea 2, columna 32:
PLS-00103: Se ha encontrado el símbolo ";" cuando se esperaba uno de los siguientes:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an al
06550. 00000 -  "line %s, column %s:n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

很抱歉我的语法错误,如果不清楚,我会跟帖给你更多信息。thx

异常处理程序用于处理在执行函数、过程或匿名块时遇到的异常(错误(。它们不处理语法错误——函数或过程甚至不会编译语法错误。

您使用的是替换变量,该变量在代码呈现给PL/SQL解析器之前由编辑器"替换"(替换(。这种错误在代码中是无法预料和处理的——如果你在提示时什么都不返回(一个空字符串(,代码本身就会在那个地方收到"nothing",并且你的函数或过程的语法会不正确,所以你会收到语法错误。您的代码在替换后看起来完全像这样:

v_input NUMBER(8) := ;

导致您所看到的语法错误。

在运行时传递值的正确方法是使用参数(用于函数或过程(,或在匿名块中绑定变量。在执行块之前,必须声明并初始化变量(在类似SQL*Plus的命令行环境中(;或者,在像Toad或SQL Developer这样的GUI中,系统会提示您输入绑定变量的值。如果按Enter键而不给出值,则会被解释为NULL,这是完全有效的。(不管这是不是你想要的,那是另一个问题。(

代码必须看起来像这样:

declare
v_input number(8) := :entry;     --  notice the leading colon  :
begin
dbms_output.put_line(v_input);
end;
/

如果你想处理NULL输入的情况,你可以用代码来做

declare
v_input number(8) := :entry;
begin
if v_input is null then
--  handle it here
else
dbms_output.put_line(v_input);   -- or whatever else you need to do
end if;
end;
/

您不需要异常来处理它;您可以在IF中使用dbms_output.put_line(注意您使用了putline,这是错误的(,如果您想停止执行,甚至可以使用raise_application_error。例如(有效值优先(:

SQL> declare
2    v_codigo varchar2(20) := '&entry';
3  begin
4    if v_codigo = ';' or v_codigo is null then
5       raise_application_Error(-20000, 'Invalid value');
6    else
7       dbms_output.put_line('You entered ' || v_codigo);
8    end if;
9  end;
10  /
Enter value for entry: Littlefoot
You entered Littlefoot
PL/SQL procedure successfully completed.

无效值:空字符串和;

SQL> /
Enter value for entry:
declare
*
ERROR at line 1:
ORA-20000: Invalid value
ORA-06512: at line 5

SQL> /
Enter value for entry: ;
declare
*
ERROR at line 1:
ORA-20000: Invalid value
ORA-06512: at line 5

SQL>

或者,要重用您自己的代码:首先声明异常(第3行(,然后使用它:

SQL> declare
2    v_codigo varchar2(20) := '&entry';
3    v_excepcion exception;
4  BEGIN
5      DBMS_OUTPUT.PUT_LINE(v_codigo);
6      IF(v_codigo = ';' or v_codigo is null) THEN
7            RAISE v_excepcion;
8      END IF;
9
10  EXCEPTION
11      when v_excepcion then
12           DBMS_OUTPUT.PUT_LINE('No has insertado un valor válido');
13  END;
14  /
Enter value for entry: Littlefoot
Littlefoot
PL/SQL procedure successfully completed.
SQL> /
Enter value for entry:
No has insertado un valor válido
PL/SQL procedure successfully completed.
SQL> /
Enter value for entry: ;
;
No has insertado un valor válido
PL/SQL procedure successfully completed.
SQL>

最新更新