简体   繁体   English

PL SQL 程序未引发异常 no_data_found

[英]PL SQL procedure not raising exception no_data_found

I have two tables as customers and orders as follows: Customers:我有两张表作为customersorders如下:客户:

+----+------+------------+
| ID | Name | Contact_no |
+----+------+------------+
|  1 | Matt | 9435112340 |
|  2 | John | 6654342312 |
|  3 | Jill | 6654342312 |
+----+------+------------+

Orders:订单:

+----------+------------+----------+------+
| Order_id | Order_date | Quantity | C_id |
+----------+------------+----------+------+
|     1011 | 09-jan-18  |       30 |    1 |
|     1012 | 09-feb-18  |      300 |    1 |
|     1013 | 09-feb-18  |      200 |    3 |
|     1111 | 09-feb-18  |      100 |    1 |
|     1016 | 09-feb-18  |       20 |    2 |
+----------+------------+----------+------+

I am writing a PL/SQL procedure which lists the id , name and contact_no of those who have the order quantity greater than 200. I am able to achieve this.我正在编写一个PL/SQL程序,其中列出了订单数量大于 200 的人的idnamecontact_no 。我能够做到这一点。 But if no records exists, then i want to print a message "No Records Found".但如果不存在记录,那么我想打印一条消息“未找到记录”。 I know that if there are no records, then oracle throws no_data_found exception.我知道如果没有记录,那么oracle会抛出no_data_found异常。 Thus, I have written my procedure according to that.因此,我已经根据它编写了我的程序。 But, though there are not records, oracle is not throwing exception and my Exception block is not executing and nothing is printing as output.但是,虽然没有记录,但oracle没有抛出异常,我的Exception块没有执行,也没有打印为 output。 Can anyone please help what is going wrong and how can i achieve the required results?任何人都可以请帮助出了什么问题,我怎样才能达到所需的结果? Below is my procedure.下面是我的程序。

DECLARE
    CURSOR cust_cur IS
    SELECT * FROM customers WHERE customers.id IN (SELECT c_id FROM orders WHERE orders.quantity>300 group by c_id);
BEGIN
    FOR cust IN cust_cur LOOP
        dbms_output.put_line('Customer_id Customer_Name Customer_Phone');
        dbms_output.put_line(cust.id||' '||cust.name||' '||cust.contact_no);
    END LOOP;
EXCEPTION
    WHEN no_data_found THEN
        dbms_output.put_line('No data found');
    WHEN OTHERS THEN
    dbms_output.put_line('Error Due To -->'|| SQLCODE||SQLERRM);
END;

I have changed the condition of quantity in SQL statement from 200 to 300, as it will return no rows.我已将 SQL 语句中的数量条件从 200 更改为 300,因为它不会返回任何行。

no_data_dound is raised only by select..into..from clause This "FOR cust IN cust_cur LOOP" is a loop and not a select statement.. loops like these are treated as bulk collect statements by oracle internally and default limit to which is 100. no_data_dound is raised only by select..into..from clause This "FOR cust IN cust_cur LOOP" is a loop and not a select statement.. loops like these are treated as bulk collect statements by oracle internally and default limit to which is 100 .

You may control data count in loop manually.您可以手动控制循环中的数据计数。 Like below.如下所示。

declare
   cursor cust_cur
   is
      select *
        from customers
       where customers.id in (  select c_id
                                  from orders
                                 where orders.quantity > 300
                              group by c_id);

   l_count   number := 0;
begin
   for cust in cust_cur
   loop
      DBMS_OUTPUT.put_line ('Customer_id Customer_Name Customer_Phone');
      DBMS_OUTPUT.put_line (
         cust.id || ' ' || cust.name || ' ' || cust.contact_no);
      l_count := 1;
   end loop;

   if l_count = 0
   then
      raise NO_DATA_FOUND;
   end if;
exception
   when NO_DATA_FOUND
   then
      DBMS_OUTPUT.put_line ('No data found');
   when others
   then
      DBMS_OUTPUT.put_line ('Error Due To -->' || SQLCODE || SQLERRM);
end;

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

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