繁体   English   中英

我在 oracle sql 服务器的 for 循环内的异常语法代码有问题

[英]I have a problem with the syntax code of exception inside the for loop in oracle sql server

当我的经理计数等于 0 时,我想添加异常。但是我的代码存在语法错误,我不知道该怎么做。

错误:

PLS-00103:在预期以下情况之一时遇到符号“例外”:

( begin case declare end exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge json_exists json_value json_query json_object json_array 06550. 00000 - "line %s,列 %s:\n%s" *原因:通常是 PL/SQL 编译错误。*操作:

FOR l in c LOOP
select count(*) into variable
    from table where job = 'Manager'
    and condition;


    if SQL%NOTFOUND
EXCEPTION
    then 
        RAISE e_my_exception;
    end if;
    //the code to check salary of employee greater than president or lower than 100 is here //

    WHEN e_my_exception THEN
    DBMS_OUTPUT.PUT_LINE('ERROR!!! One department has no manager ');  
END LOOP;

我希望打印出 'ERROR:!! 一个部门没有经理 ' 但反而有一个错误:

您没有正确处理异常。 我建议如下:

FOR l in c LOOP
  BEGIN
    select count(*)
      into variable
      from table where job = 'Manager'
      and condition;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.PUT_LINE('ERROR!!! One department has no manager');  
  END;
END LOOP;

没有“oracle sql 服务器”之类的东西(正如您的标题所示)。 它是“Oracle”或“(Microsoft) SQL 服务器”。 您发布的代码是 Oracle,所以我建议您修复标题。


现在,您的问题:当那里什么都没有(但结果为 0)时, count(*) 将无法工作“找不到数据”或“sql%notfound” - 请参阅演示:

SQL> select count(*) from dual where 1 = 2;

  COUNT(*)
----------
         0

SQL>

那么你可以做这样的事情:

FOR l in c LOOP
  begin
    select count(*) into variable
      from table where job = 'Manager'
      and condition;

    if variable = 0 then
       RAISE e_my_exception;
    end if;
    --the code to check salary of employee greater than president or lower than 100 is here //

  exception
    WHEN e_my_exception THEN
      DBMS_OUTPUT.PUT_LINE('ERROR!!! One department has no manager ');  
END LOOP;  

这样的代码会显示消息(如果您的工具支持它),但循环会继续循环,即您的代码不会停止。


不过,由于您实际上并没有筹集任何资金,因此更简单的选择是

FOR l in c LOOP
    select count(*) into variable
      from table where job = 'Manager'
      and condition;

    if variable = 0 then
       DBMS_OUTPUT.PUT_LINE('ERROR!!! One department has no manager ');  
    end if;
    --the code to check salary of employee greater than president or lower than 100 is here //
END LOOP;  

您以错误的格式实现它,如下所示...

DECLARE 
   <declarations section> 
BEGIN 
   <executable command(s)> //your logic
EXCEPTION 
   <exception handling> 
END;

和循环以这种方式实现......仅举个例子

DECLARE 
   i number(1); 
   j number(1); 
BEGIN 
   FOR i IN 1..3 LOOP 
         dbms_output.put_line('i is: '|| i ); 
   END loop; 
END; 

对于处理异常,请参阅此...

    DECLARE 
   c_id customers.id%type := 8; 
   c_name customerS.Name%type; 
   c_addr customers.address%type; 
BEGIN 
   SELECT  name, address INTO  c_name, c_addr 
   FROM customers 
   WHERE id = c_id;  
   DBMS_OUTPUT.PUT_LINE ('Name: '||  c_name); 
   DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr); 

EXCEPTION 
   WHEN no_data_found THEN 
      dbms_output.put_line('No such customer!'); 
   WHEN others THEN 
      dbms_output.put_line('Error!'); 
END; 

暂无
暂无

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

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