带条件运算符的PL / SQL条件编译

时间:2015-06-15 06:13:04

标签: oracle plsql

我们可以在ORACLE条件编译中使用像“AND”,“OR”这样的条件运算符吗?

$IF $$con1 AND $$con2 $THEN 

$END

4 个答案:

答案 0 :(得分:2)

是的,您可以尝试以下代码

declare

 $IF 1 = 1 AND  1= 1 $THEN
   cursor c1 is
   select 'A' from dual;
 $ELSE
   cursor c2 is
   select 'B' from dual;
 $END

begin

 dbms_output.put_line('Working');
 open c2;
 close c2;

end;

答案 1 :(得分:1)

根据Oracle Doc

  

使用条件编译,您可以自定义功能   PL / SQL应用程序,无需删除任何源代码

$IF boolean_static_expression $THEN text
  [ $ELSIF boolean_static_expression $THEN text ]
  [ $ELSE text ]
$END

并且,BOOLEAN static expressions包括:

TRUE, FALSE, and the literal NULL

x > y, x < y, x >= y, x <= y, x = y, and x <> y where x and y are PLS_INTEGER static expressions

NOT x, x AND y, x OR y, x > y, x >= y, x = y, x <= y, x <> y where x and y are BOOLEAN static expressions

x IS NULL and x IS NOT NULL where x is a static expression

所以,答案是

答案 2 :(得分:1)

这是一个使用条件编译的简单程序:

create or replace procedure cc_test is
    n simple_integer := 10;
begin
    $if $$tst_flag = 1 $then
        n := n + 10;
    $elsif $$tst_flag = 2 $then
        n := n + 15;
    $elsif $$tst_flag = 3 $then
        n := n - 5;
    $else   
        n := 42;
    $end
    dbms_output.put_line ('n = ' || to_char(n));
end;
/

它的默认行为是:

...
Procedure created.

SQL> exec cc_test
n = 42

PL/SQL procedure successfully completed.

SQL> 

我们可以通过设置条件编译标志并重新编译过程来改变行为。

SQL> ALTER SESSION SET PLSQL_CCFLAGS = 'tst_flag:1';

Session altered.

SQL> exec cc_test   
n = 42

PL/SQL procedure successfully completed.

SQL> alter procedure cc_test compile;

Procedure altered.

SQL> exec cc_test
n = 20

PL/SQL procedure successfully completed.

SQL> ALTER SESSION SET PLSQL_CCFLAGS = 'tst_flag:3';

Session altered.

SQL>  alter procedure cc_test compile;

Procedure altered.

SQL>  exec cc_test
n = 5

PL/SQL procedure successfully completed.

SQL> 

所以,问题是,我们可以结合多个标志的效果,像这样吗?

create or replace procedure cc_test_2 is
    n simple_integer := 10;
begin
    $if $$tst_flag_1 = 1 and $$tst_flag_2 = 1 $then
        n := 12;
    $elsif $$tst_flag_1 = 2 and $$tst_flag_2 = 3 $then
        n := 23;
    $else   
        n := 42;
    $end
    dbms_output.put_line ('n = ' || to_char(n));
end;
/

是的,我们可以:

SQL> exec cc_test_2
n = 42

PL/SQL procedure successfully completed.

SQL> ALTER SESSION SET PLSQL_CCFLAGS = 'tst_flag_1:2, tst_flag_2:3';

Session altered.

SQL> alter procedure cc_test_2 compile;

Procedure altered.

SQL> exec cc_test_2
n = 23

PL/SQL procedure successfully completed.

SQL> 

此行为仅限于会话。通过在编译时设置标志使其永久化:

SQL> alter procedure cc_test_2 compile  PLSQL_CCFLAGS = 'tst_flag_1:2, tst_flag_2:3';

Procedure altered.

SQL>  exec cc_test_2
n = 23

PL/SQL procedure successfully completed.

SQL> conn apc
Enter password: 
Connected.
SQL> set serveroutput on
SQL> exec cc_test_2
n = 23

PL/SQL procedure successfully completed.

SQL> 

答案 3 :(得分:0)

是条件运算符可以在条件编译中使用。