【发布时间】:2016-01-01 02:07:28
【问题描述】:
以下 PL/SQL 代码显示了函数 ensure(...) 以及我将如何在 Oracle 11g 中使用它。
declare
valid boolean;
function ensure(b boolean, failure_message varchar) return boolean is begin
if not b then
dbms_output.put_line(failure_message);
return false;
else
return true;
end if;
end ensure;
begin
valid := true;
valid := valid
and ensure(1=1, 'condition 1 failed')
and ensure(1=2, 'condition 2 failed')
and ensure(2=3, 'condition 3 failed');
if not valid then
dbms_output.put_line('some conditions failed, terminate the program');
return;
end if;
dbms_output.put_line('do the work');
end;
/
我想使用ensure(...)来预验证一组条件,只有所有条件都通过后,程序才允许工作。
我希望程序评估每个 ensure(...),即使前面的 ensure(...) 返回 false,以便为每个失败的条件打印 failure_message。
问题在于 Oracle 使用短路评估并忽略返回 false 之后的其余条件。例如,上面的程序打印以下消息。
condition 2 failed
some conditions failed, terminate the program
如何告诉 Oracle 不使用短路评估,以便上述程序打印以下消息。
condition 2 failed
condition 3 failed
some conditions failed, terminate the program
【问题讨论】:
-
当你使用函数时,最好从面向函数的编程角度来考虑;即一个函数应该没有副作用;它是否被执行并不重要。如果你需要一个动作发生,它应该作为一个过程运行,而不是作为一个函数。
标签: plsql oracle11g short-circuiting