Oracle PL / SQL条件声明?

shan山

我们可以在pl / sql函数或过程中进行变量的条件声明吗?例如,我们可以做以下事情吗:

CREATE OR REPLACE FUNCTION get_total_sales(
    in_year PLS_INTEGER
) 
RETURN NUMBER
IS
    IF condtion THEN
       variable_name_1 BINARY_FLOAT;
    ELSE
       variable_name_2 NUMBER;
    END;

BEGIN
   --- Function Logic
END;

如果condition满足,则需要声明一个名为的变量variable_name_1,否则,我不想声明它,而需要声明一个名为的变量variable_name_2

皮佛

Oracle 10开始就有可能了

例:

SQL> CREATE OR REPLACE PROCEDURE test AS
  2    $IF $$test_on $THEN
  3      v1 BINARY_FLOAT;
  4    $ELSE
  5     v2 NUMBER;
  6    $END
  7    v boolean;
  8  BEGIN
  9   v := true;
 10  END test;
 11  /

Procedure created.

SQL> show errors
No errors.
SQL> --
SQL> ALTER PROCEDURE test COMPILE PLSQL_CCFLAGS = 'test_on:TRUE' REUSE SETTINGS;

Procedure altered.

SQL> --
SQL> BEGIN
  2  
  3    DBMS_PREPROCESSOR.print_post_processed_source (
  4      object_type => 'PROCEDURE',
  5      schema_name => 'SYSTEM',
  6      object_name => 'TEST');
  7  END;
  8  /
PROCEDURE test AS
v1 BINARY_FLOAT;
v boolean;
BEGIN
v := true;
END test;

PL/SQL procedure successfully completed.

SQL> show errors
No errors.
SQL> --
SQL> ALTER PROCEDURE test COMPILE PLSQL_CCFLAGS = 'test_on:FALSE' REUSE SETTINGS;

Procedure altered.

SQL> --
SQL> BEGIN
  2  
  3    DBMS_PREPROCESSOR.print_post_processed_source (
  4      object_type => 'PROCEDURE',
  5      schema_name => 'SYSTEM',
  6      object_name => 'TEST');
  7  END;
  8  /
PROCEDURE test AS
v2 NUMBER;
v boolean;
BEGIN
v := true;
END test;

PL/SQL procedure successfully completed.

SQL>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章