Solved

How to write exception while using in explicit Cursor?

Posted on 2008-06-17
9
491 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:Fekrat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good Result Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now