Solved

How to write exception while using in explicit Cursor?

Posted on 2008-06-17
9
493 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help With Database JOIN 7 28
Need help on t-sql 2012 10 55
Creation date for a PDB 5 23
Oracle collections 15 23
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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.

821 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