设置除以 0 返回 0

Zed*_*eda 2 oracle plsql divide-by-zero

我突然想到 PL/SQL 可能有一种更简洁的方法来做到这一点:

我有一个程序可以被零除,但在所有情况下我都希望它返回 0。我当前的逻辑如下所示:

if bar <> 0 then
    foobar := foo/bar;
else
    foobar := 0;
end if;
Run Code Online (Sandbox Code Playgroud)

有谁知道更简单/更便宜的方法来做到这一点?我对 PL/SQL 还很陌生,我看到我可以使用异常处理程序,但这似乎跳过了操作。所以我想我可以破例zero_divide然后做:

foobar := 0;
foobar := foo/bar;
Run Code Online (Sandbox Code Playgroud)

不过,我担心这并不容易遵循。感谢您的任何见解!

Goo*_*Dok 5

请尝试以下操作:

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;
Run Code Online (Sandbox Code Playgroud)

输出:

0
Run Code Online (Sandbox Code Playgroud)