473,320 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using exception handling in a Loop w/ Cursor

4
I'm trying to use an exception handler when the select statement tries to access an invalid item_id. I tried and with this code, it runs but throws nothing. I later found out that cursor will make it run regards if no rows are returned.
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.   CURSOR orderlist IS
  3.     SELECT distinct inv_id, b.item_id, item_desc, inv_price
  4.     FROM inventory a, item b
  5.     WHERE a.item_id=b.item_id AND b.item_id=10
  6.     ORDER BY inv_id;
  7.   output orderlist%ROWTYPE;
  8. BEGIN
  9.   OPEN orderlist;
  10.   LOOP
  11.     BEGIN
  12.     FETCH orderlist INTO output;
  13.     EXIT WHEN orderlist%NOTFOUND;
  14.     DBMS_OUTPUT.PUT_LINE(output.inv_id || ' ' || output.item_id || ' ' || output.item_desc || ' ' || output.inv_price);
  15.     EXCEPTION
  16.       WHEN NO_DATA_FOUND THEN
  17.         DBMS_OUTPUT.PUT_LINE('test');
  18.       WHEN OTHERS THEN
  19.     DBMS_OUTPUT.PUT_LINE('SQLERRM');
  20.     END;
  21.   END LOOP;
  22.   CLOSE orderlist;
  23. END;
Then I did some searching and tried to initiate a count for the loop and if nothing throw the handler but no luck. Error that keeps popping up is the last line. It says it notice ";" but expect LOOP.
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.   CURSOR orderlist IS
  3.     SELECT distinct inv_id, b.item_id, item_desc, inv_price
  4.     FROM inventory a, item b
  5.     WHERE a.item_id=b.item_id AND b.item_id=10
  6.     ORDER BY inv_id;
  7.   output orderlist%ROWTYPE;
  8.   cnt NUMBER;
  9. BEGIN
  10.   OPEN orderlist;
  11.   cnt := 0;
  12.   LOOP
  13.     BEGIN
  14.     FETCH orderlist INTO output;
  15.     EXIT WHEN orderlist%NOTFOUND;
  16.     DBMS_OUTPUT.PUT_LINE(output.inv_id || ' ' || output.item_id || ' ' || output.item_desc || ' ' || output.inv_price);
  17.     cnt := cnt + 1;
  18.   END LOOP;
  19.   CLOSE orderlist;
  20.   IF cnt = 0 THEN
  21.     DBMS_OUTPUT.PUT_LINE('NOTHING FOUND');
  22.   END IF;
  23. END;
So I research again and found out that works with for loops and it works but how do I output the data from the table if I place a correct value for item_id
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.   --CURSOR orderlist IS
  3.  
  4.   --output orderlist%ROWTYPE;
  5.   cnt NUMBER;
  6. BEGIN
  7.   --OPEN orderlist;
  8.   cnt := 0;
  9.   FOR x IN (SELECT distinct inv_id, b.item_id, item_desc, inv_price
  10.     FROM inventory a, item b
  11.     WHERE a.item_id=b.item_id AND b.item_id=10
  12.     ORDER BY inv_id)
  13.   LOOP
  14.     --BEGIN
  15.     --FETCH orderlist INTO output;
  16.     --EXIT WHEN orderlist%NOTFOUND;
  17.     --DBMS_OUTPUT.PUT_LINE(output.inv_id || ' ' || output.item_id || ' ' || output.item_desc || ' ' || output.inv_price);
  18.     cnt := cnt + 1;
  19.   END LOOP;
  20.   --CLOSE orderlist;
  21.   IF cnt = 0 THEN
  22.     DBMS_OUTPUT.PUT_LINE('NOTHING FOUND');
  23.   END IF;
  24. END;
Apr 8 '13 #1
1 16623
Beeb
4
Never mind I figured it out. Here's my code incase anyone has the same question
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.   CURSOR orderlist IS
  3.     SELECT distinct inv_id, b.item_id, item_desc, inv_price
  4.     FROM inventory a, item b
  5.     WHERE a.item_id=b.item_id AND b.item_id=1
  6.     ORDER BY inv_id;
  7.   output orderlist%ROWTYPE;
  8.   error EXCEPTION;
  9. BEGIN
  10.   OPEN orderlist;
  11.   LOOP
  12.     FETCH orderlist INTO output;
  13.     IF orderlist%NOTFOUND
  14.       THEN
  15.          RAISE error;
  16.       END IF;
  17.     DBMS_OUTPUT.PUT_LINE(output.inv_id || ' ' || output.item_id || ' ' || output.item_desc || ' ' || output.inv_price);
  18.   END LOOP;
  19.   CLOSE orderlist;
  20.   EXCEPTION
  21.    WHEN error THEN
  22.     DBMS_OUTPUT.PUT_LINE('Invalid Item ID');
  23. END;
Apr 8 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: A. Saksena | last post by:
Hi, Do anybody have an idea of the performance penalty while using exception handling (specially with g++) Abhishek
4
by: zzfreddybb | last post by:
We are using HP aCC compiler on a HP Itanium box ( 11.23) We are having some severe performance hits using exception handling ( try/catch ) scenarios. The online aCC documentation says: HP...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
9
by: C# Learner | last post by:
Some time ago, I remember reading a discussion about the strengths and weaknesses of exception handling. One of the weaknesses that was put forward was that exception handling is inefficient (in...
2
by: Petr Jakes | last post by:
I am a little bit confused by all possibilities for exceptions handling in Python (probably because I am not skilled enough??) I did try to search trough this list and reading Python tutorial about...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
6
by: Mohan | last post by:
Hi, I am learning the Exception Handling in C++. I wrote a small program using Exception Handling. I am using Vistual Studio 6. It is working fine in Win32 Debug build, but it is not...
2
by: amohammad | last post by:
Hi , All I am a linux user. As we all know using exception handling we can catch like divide by zero, floating point exception etc in C++. Can we catch a segmentation fault and kill signal using...
0
by: theinvisibleGhost | last post by:
I've been developing in C#/.NET and occasionally Java for about 3 years now. I understand most of the principles of these languages fairly well. I understand how to use exception handling itself,...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.