[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to write exception while using in explicit Cursor?

Posted on 2008-06-17
9
Medium Priority
?
502 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

650 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