if / else条件逻辑与异常处理

时间:2016-09-15 22:42:09

标签: oracle plsql refactoring

如果我必须根据产品的发布版本运行PL / SQL代码,是否最好添加if / else条件逻辑或通过特殊处理来处理它?

IF release_version > 5 THEN
    execute some SQL query only for release > 5 because a certain column was only introduced in ver 6;
ELSE
    execute another SQL query;
END IF;

1 个答案:

答案 0 :(得分:1)

最佳做法是使用“On Conditional Compilation”

Conditional Compilation in Oracle

这样只会编译你的版本所需的代码。

create or replace package product as
   version   number := 5;
end;
/

create or replace procedure compiletime as
begin
   $if product.version=5 $then
      dbms_output.put_line ('I am version 5');
   $elsif product.version=6 then
      dbms_output.put_line('I am version 6');
   $else
      dbms_output.put_line('Some other version');
   $end
end compiletime;

在此示例中,仅“ dbms_output.put_line('我是版本5'); ”将被编译。休息将被丢弃。