Solved

How to write exception while using in explicit Cursor?

Posted on 2008-06-17
9
494 Views
Last Modified: 2013-12-19
Hi,
  I have a table like sample_t2. It contains the value like below.
id          name      sal
1           A           1000
1           B           2000
1           C           3000
2           C           3000
          I have wriiten one procedure like when i give ID as input parameter it return the name and sal values. If i give id 1 it will return all id 1 values. For that i have used cursor. I am attaching the procedure in the document file. When i give id 3, it is not returning any exception like no data found, etc. For more information see the document.
sample-pro-id-procedure.doc
0
Comment
  • 3
  • 3
  • 3
9 Comments
 
LVL 4

Expert Comment

by:Fekrat
ID: 21810954
It is known that explicit cursors do not throw no data found exception, only select into's do. It is not a ref cursor problem that there is does not throw an exception, it is a cursor way of doing things. I'm sure that it is considered to be a bug that is has no rows. I think you should have an if statment before you're loop that checks if "cur_sample_id%NOTFOUND" and throws you're own excpetion and handle it in the exception section. Hope this helps.
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 250 total points
ID: 21811006
as already updated, only select .. into .. statements raise no_data_found in pl/sql and not explicit cursors.

You can either do

a) have an explicit select statement and before the loop start itself to check for the id whatever you are passing to see whether records are there are not but add in the where clause " and rownum = 1 " as well because if there are more than 1 record, then you will too_many_rows exception. If you add rownum = 1, then select will bring only 1 record and will not get this error.
    Also if you have the select before the open cursor, then you do not need to execute that code if the select itself is going to raise no_data_found.

b) or you can even interate a number variable to count the number of records after opening and fetching and see whether the count is 0 and if yes, then display the message or define a user defined exception and raise it and if the counter is not 0 then it means that your cursor has brought records
0
 

Author Comment

by:Suriyaraj_Sudalaiappan
ID: 21811174
Hi,
   This is my updated procedure. I am using the count after fetching the records. But again it is giving the same. Please check it.
CREATE OR REPLACE PROCEDURE proc_cur_sam_2
           (i_id IN SAMPLE_T2.ID%TYPE,
          o_id_results OUT VARCHAR2,
            o_errbuf OUT VARCHAR2,
            o_retcode OUT NUMBER)
IS
        l_name varchar2(10);
        l_sal number;
        a EXCEPTION;
--      f BOOLEAN := TRUE;
CURSOR c
IS
      SELECT name, sal
      FROM sample_t2
      WHERE id = i_id;

BEGIN
       o_retcode := 0;
       o_id_results := '';                  
       BEGIN
               OPEN c;
              LOOP
             
                      FETCH c INTO l_name, l_sal;
                    EXIT WHEN c%NOTFOUND;
                                        IF c%ROWCOUNT = 0
                    THEN
                            DBMS_OUTPUT.PUT_LINE('CURSOR_EMPTY');
                            RAISE a;
                    ELSE
                            DBMS_OUTPUT.PUT_LINE('CURSOR_OK');
                    END IF;
--                    f := FALSE;
                      IF o_id_results IS NOT NULL
                    THEN
                          o_id_results := o_id_results || '| ';
                    END IF;
                     o_id_results := o_id_results|| l_name ||','|| l_sal;
             END LOOP;
--      IF f THEN
--      RAISE a;
--      END IF;
EXCEPTION
      WHEN a THEN
             o_retcode := 2;
             o_id_results := '';
             o_errbuf := 'ERROR:Error occured while retrieving Best Technial Match:' || 'no_data_found' ;
       WHEN OTHERS
            THEN
                  o_retcode := 2;
                  o_id_results := '';
                  o_errbuf := 'ERROR:Error occured while retrieving Best Technial Match:' || SQLERRM ;                        
                  
                  DBMS_OUTPUT.put_line (o_errbuf);
      END;
END proc_cur_sam_2;
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 21811195
FETCH c INTO l_name, l_sal;
                    EXIT WHEN c%NOTFOUND;
                                        IF c%ROWCOUNT = 0
                    THEN
                            DBMS_OUTPUT.PUT_LINE('CURSOR_EMPTY');
                            RAISE a;
                    ELSE
                            DBMS_OUTPUT.PUT_LINE('CURSOR_OK');
                    END IF;

change as shown below :

 FETCH c INTO l_name, l_sal;
                   IF c%ROWCOUNT = 0
                    THEN
                            DBMS_OUTPUT.PUT_LINE('CURSOR_EMPTY');
                            RAISE a;
                    ELSE
                            DBMS_OUTPUT.PUT_LINE('CURSOR_OK');
                    END IF;
                    EXIT WHEN c%NOTFOUND;
0
 
LVL 4

Expert Comment

by:Fekrat
ID: 21811525
Just to keep you aware the ROWCOUNT attribute doesn't give the real row count until you have iterated through the entire cursor. In other words, you shouldn't rely on this attribute to tell you how many rows are in a cursor after it is opened.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 21811554
FETCH c INTO l_name, l_sal;
                   IF c%ROWCOUNT = 0
                    THEN
                            DBMS_OUTPUT.PUT_LINE('CURSOR_EMPTY');
                            RAISE a;
                    ELSE
                            DBMS_OUTPUT.PUT_LINE('CURSOR_OK');
                    END IF;
                    EXIT WHEN c%NOTFOUND;

I think the above code is fine because we are just checking whether we have got a record or not after fetching and we are not counting the number of records/comparing something to count of records.

I tested and it works fine without any issues.
0
 

Author Comment

by:Suriyaraj_Sudalaiappan
ID: 21811746
Hi i did the following and it also gives the answer. Please confirm me which one will not give error in the future??.

 OPEN c;
  LOOP
  FETCH c INTO l_name, l_sal;
  EXIT WHEN c%NOTFOUND;
  IF o_id_results IS NOT NULL
  THEN
  o_id_results := o_id_results || '| ';
  END IF;
  o_id_results := o_id_results|| l_name ||','|| l_sal;
 END LOOP;
  IF c%ROWCOUNT = 0
  THEN
  DBMS_OUTPUT.PUT_LINE('CURSOR_EMPTY');
  RAISE a;
  ELSE
  DBMS_OUTPUT.PUT_LINE('CURSOR_OK');
  END IF;
CLOSE c;
0
 
LVL 4

Expert Comment

by:Fekrat
ID: 21811798
Would you mind trying this instead
FETCH c INTO l_name, l_sal;
                   IF c%NOTFOUND
                    THEN
                            DBMS_OUTPUT.PUT_LINE('CURSOR_EMPTY');
                            RAISE a;
                    ELSE
                            DBMS_OUTPUT.PUT_LINE('CURSOR_OK');
                    END IF;

Open in new window

0
 

Author Closing Comment

by:Suriyaraj_Sudalaiappan
ID: 31468227
Good Result Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question