It occurred to me that PL/SQL might have a more concise way of doing this:
I have a program where division-by-zero can occur, but in all cases I want this to return 0. My current logic looks like:
if bar <> 0 then
foobar := foo/bar;
else
foobar := 0;
end if;
Does anyone know of an easier/cheaper way to do this ? I'm fairly new to PL/SQL and I saw that I could use an exception handler, but that appears to skip the operation. So I think I could make an exception for zero_divide
and then just do:
foobar := 0;
foobar := foo/bar;
I worry that isn't as easy to follow, though. Thanks for any insight!
Try the following:
declare
foo integer := 1;
bar integer := 0;
foobar integer;
begin
foobar := case bar when 0 then 0 else foo/bar end;
dbms_output.put_line(foobar);
end;
Output:
0