Doubt in Exception handling. [message #341788] |
Wed, 20 August 2008 05:51 |
arunshrish
Messages: 74 Registered: May 2008 Location: Chennai
|
Member |
|
|
Hi all,
I have a table named classes with description ,
SQL> desc classes
Name Null? Type
----------------------------------------------------- -------- ------------------
DEPARTMENT NOT NULL CHAR(3)
COURSE NOT NULL NUMBER(3)
DESCRIPTION VARCHAR2(2000)
MAX_STUDENTS NUMBER(3)
CURRENT_STUDENTS NUMBER(3)
NUM_CREDITS NUMBER(1)
ROOM_ID NUMBER(5)
I created a procedure named 'limit' as follows which displays an error that the max student limit has been exceeded as follows,
create or replace procedure limit (p_no IN number,p_dep IN varchar2) is
v_cur classes.current_students%type;
v_max classes.max_students%type;
e_toomany exception;
begin
select current_students,max_students into v_cur,v_max from classes where department='p_dep' and course=p_no;
if v_cur>v_max then raise e_toomany;
end if;
exception
when e_toomany then
dbms_output.put_line('There are currently ' || v_cur ||' no of students which is higher than the limit of ' ||v_max);
when others then
dbms_output.put_line('students count is fine at present!!!!');
end;
/
Procedure created.
Now my doubt is when i issue the following ,
sql>exec limit(101,'HIS');
the message getting displayed is 'students count is fine at present!!!!'
But, I was expecting the error message 'There are currently 31 no of students which is higher than the limit of 30'
As, the classes table had the max students as 30 and current_students as 31 for the department and course values of 'HIS' and 101 respectively (please note the current_students far exceeds the max_students count)...
Pls help me out.
Thanks,
Arun.
|
|
|
|
Re: Doubt in Exception handling. [message #341802 is a reply to message #341800] |
Wed, 20 August 2008 06:28 |
arunshrish
Messages: 74 Registered: May 2008 Location: Chennai
|
Member |
|
|
Hi Michel,
I went thro the instructions,will follow it in the future. I wanted to ask why its necessary to remove the line that u specified ie.,
when others then
dbms_output.put_line('students count is fine at present!!!!');
Thanks.
|
|
|
|
Re: Doubt in Exception handling. [message #341811 is a reply to message #341806] |
Wed, 20 August 2008 06:58 |
arunshrish
Messages: 74 Registered: May 2008 Location: Chennai
|
Member |
|
|
Hi Michel,
I removed the line and executed as you said,
CREATE OR REPLACE PROCEDURE Limit
(p_No IN NUMBER,
p_dep IN VARCHAR2)
IS
v_Cur Classes.Current_Students%TYPE;
v_Max Classes.Max_Students%TYPE;
e_TooMany EXCEPTION;
BEGIN
SELECT Current_Students,
Max_Students
INTO v_Cur,
v_Max
FROM Classes
WHERE Department = 'p_dep'
AND Course = p_No;
IF v_Cur > v_Max THEN
RAISE e_TooMany;
END IF;
EXCEPTION
WHEN e_TooMany THEN
dbms_Output.Put_Line('There are currently '
||v_Cur
||' no of students which is higher than the limit of '
||v_Max);
END;
/
But while executing the procedure I get the following error,
exec limit(101,'HIS');
BEGIN limit(101,'HIS'); END;
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "SCOTT.LIMIT", line 6
ORA-06512: at line 1
But the values 101 and HIS are very well present in the classes table..
Thanks,
Arun.
|
|
|
|
|
|
|
Re: Doubt in Exception handling. [message #342088 is a reply to message #342012] |
Thu, 21 August 2008 05:26 |
arunshrish
Messages: 74 Registered: May 2008 Location: Chennai
|
Member |
|
|
Hi Frank,
Cool,i didnt think in your angle, thanks..i tried out your strategy , hope you give me an 'A' grade for this??!!
CREATE OR REPLACE PROCEDURE Limit_Violation
(p_No NUMBER,
p_dep VARCHAR2)
IS
v_Cur Classes.Current_Students%TYPE;
v_Max Classes.Max_Students%TYPE;
BEGIN
SELECT Current_Students,
Max_Students
INTO v_Cur,
v_Max
FROM Classes
WHERE Department = p_dep
AND Course = p_No
AND Current_Students > Max_Students;
dbms_Output.Put_Line('For department '
||p_dep
||' current students count is '
||v_Cur
||' which is a violation of max student limit of '
||v_Max);
EXCEPTION
WHEN No_Data_Found THEN
dbms_Output.Put_Line('The total student count is normal!!');
END;
/
SQL> exec limit_violation(101,'HIS');
For department HIS current students count is 99 which is a violation of max
student limit of 30
PL/SQL procedure successfully completed.
Thanks,
Arun.
|
|
|
|