Home » SQL & PL/SQL » SQL & PL/SQL » while loop (Oracle Database 11gR2, Windows XP SP3)
while loop [message #556428] Mon, 04 June 2012 04:50 Go to next message
Roger22
Messages: 98
Registered: April 2009
Location: Brasov, ROMANIA
Member
Hi,
I have the code:
declare
   subtype my_type is pls_integer range 0..1000;
   a my_type;
   b my_type;
   c my_type;
begin
   -- let's say i initialize a with 900, b=800 and c=1000
   a:=a-1;
   b:=b-1;
   c:=c-1;
exception
  when others then
    raise_application_error(-20114, 'Out of limit!');
end;
/


My question is how can i raise that error, only if ALL these 3 variables are outside of limit? so, first if b will be 0 and i try do decrement, if there are any other variables to decrement(in legal range), i should not raise the error (but only if all of them become outside of limit).

Thanks!

[Updated on: Mon, 04 June 2012 04:54]

Report message to a moderator

Re: while loop [message #556434 is a reply to message #556428] Mon, 04 June 2012 05:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
1) Most important:
exception
  when others then
    raise_application_error(-20114, 'Out of limit!');

Is a bug. Read WHEN OTHERS

2)
Quote:
only if ALL these 3 variables are outside of limit


Trap the exception at all these ones and increment a counter, if the counter is in the end set to the number of variable then raise an exception.

Regards
Michel


Re: while loop [message #556440 is a reply to message #556434] Mon, 04 June 2012 05:38 Go to previous message
_jum
Messages: 577
Registered: February 2008
Senior Member
Only trap the correct exception "VALUE_ERROR" and do as @Michel said.
If You have to check underflow/overflow seperate, you could use least/greatest as in the example:
SET SERVEROUTPUT ON SIZE 900000;

declare
   subtype my_type is pls_integer range 0..1000;
   a pls_integer;
   b pls_integer;
   c pls_integer;
   d my_type;
begin
   -- let's say i initialize a with 900, b=800 and c=1000
   a :=   0;
   b :=   0;
   c :=   1;
   a := a-1;
   b := b-1;
   c := c-1;
   -- no underflow of c
   d := greatest(a,b,c);
   a := a-1;
   b := b-1;
   c := c-1;
   -- now underflow
   d := greatest(a,b,c);
   exception when VALUE_ERROR 
     then raise_application_error(-20114, 'Out of limit! a='||a||' b='||b||' c='||c);
end;

ORA-20114: Out of limit! a=-2 b=-2 c=-1
Previous Topic: ORA-01427: single-row subquery returns more than one row
Next Topic: Please help in query
Goto Forum:
  


Current Time: Wed Aug 27 10:11:05 CDT 2025