gpt4 book ai didi

database - Oracle PL/SQL 条件声明?

转载 作者:行者123 更新时间:2023-12-01 21:30:41 25 4
gpt4 key购买 nike

我们可以在 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;

如果满足条件,我需要声明一个名为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>

关于database - Oracle PL/SQL 条件声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62589100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com