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

时间:2019-11-01 18:59:01

标签: sql oracle

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

错误:

  

PLS-00103:预期出现以下情况之一时遇到符号“ EXCEPTION”   以下:

     

(如果case循环mod为null,则开始情况为goto声明结束出口   使用<<继续时提高收益选择更新   关闭当前删除获取锁插入打开回滚保存点集   SQL执行提交所有合并管道清除json_exists json_value   json_query json_object json_array   06550。00000-“%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;

我希望打印出“错误!!!!一个部门没有经理',但是出现错误:

3 个答案:

答案 0 :(得分:0)

您没有正确处理异常。我建议以下内容:

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;

答案 1 :(得分:0)

没有“ oracle sql server”之类的东西(正如您的标题所示)。它是“ Oracle”或“(Microsoft)SQL Server”。您发布的代码是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;  

答案 2 :(得分:0)

您正在以错误的格式实施它,如下所示...

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;