| Handling Exceptions! [message #571651] |
Wed, 28 November 2012 09:34  |
 |
akull
Messages: 27 Registered: July 2012 Location: Argentina
|
Junior Member |
|
|
Hi guys! I have an issue with handling exceptions, here is my problem. If i make an anominous block with a cursor and if the cursor didn't retrieve any data, it should trigger the exception no_data_found if I am not mistaken but it didn't show any data wich is correct because the ticket that I am trying to retrieve does not exist in the BD but it does not trigger the exception either.
Help you help me out ?
Here is the code
DECLARE
cursor c_data is
select * from audit_wa
where ticket = '0000000000000000000000000000001';
BEGIN
for i in c_data loop
DBMS_OUTPUT.PUT_LINE('ID: ' || i.id);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line ('Ticket does not exist');
END;
Thanks in advance.-
[Updated on: Wed, 28 November 2012 09:36] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
| Re: Handling Exceptions! [message #571664 is a reply to message #571660] |
Wed, 28 November 2012 11:20   |
 |
Michel Cadot
Messages: 54226 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
It is over-complicated, so much that I'm not sure it is correct.
For instance:
Quote:IF (c_data%rowcount > 0) THEN
EXIT WHEN c_data%notfound;
If rowcount > 0 then you can't have "c_data%notfound;", so this line is useless.
Do you try to obfuscate what your code does?
Do you try to make the maintenance guy's job a nightmare?
Why don't you just have a counter in your cursor FOR loop?
Or check rowcount at the end of the loop?
SQL> declare curs sys_refcursor; rec emp%rowtype;
2 begin
3 open curs for select * from emp;
4 loop
5 fetch curs into rec;
6 exit when curs%notfound;
7 end loop;
8 dbms_output.put_line('Rowcount is: '||curs%rowcount);
9 close curs;
10 end;
11 /
Rowcount is: 14
PL/SQL procedure successfully completed.
Regards
Michel
[Updated on: Wed, 28 November 2012 11:23] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|