Link to home
Start Free TrialLog in
Avatar of Steve Berger
Steve BergerFlag for United States of America

asked on

How to write exception while using in explicit Cursor?

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
Avatar of Fekrat El Wehedi
Fekrat El Wehedi
Flag of Canada image

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.
ASKER CERTIFIED SOLUTION
Avatar of Naveen Kumar
Naveen Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Berger

ASKER

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;
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;
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.
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.
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;
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

Good Result Thanks