不满足条件时忽略替换变量

时间:2018-01-29 10:18:56

标签: sql oracle plsql

如果不满足条件,如何忽略替换变量,但是 Oracle总是从用户那里提示它吗?

declare
    vari_axu number;
begin
    if &param = 1 then
        dbms_output.put_line ('work here ');

        select count(*) into vari_axu from &mytable;
        dbms_output.put_line (vari_axu);
        return;
    end if;

    dbms_output.put_line ('do not work' );
end;

即使不满足条件&param = 1,Oracle仍会提示&mytable。 如何避免呢?

1 个答案:

答案 0 :(得分:1)

Windows的SQL * Plus脚本:

set define "&"
set verify off
define mytable = dual

accept param prompt "Enter option 1-9: "

-- Generate a script named prompt_for_tablename.sql which prompts the user for a table name
-- or does nothing, depending on the value of &param:
set termout off
column mytable new_value mytable
set head off feedback off
spool prompt_for_tablename.sql
select 'accept mytable char prompt "Enter table name: "' as prompt from dual where &param = 1;
spool off
set termout on head on feedback on

@prompt_for_tablename.sql
host del prompt_for_tablename.sql

declare
    vari_axu number;
begin
    if &param = 1 then
        dbms_output.put_line ('work here');
        select count(*) into vari_axu from &mytable ;
        dbms_output.put_line('Count of &mytable: ' || vari_axu);
        return;
    end if;

    dbms_output.put_line ('do not work' );
end;
/

如果用户在第一次提示时输入1,则生成的脚本prompt_for_tablename.sql将提示输入表名。对于任何其他值,它将什么都不做。

然后运行prompt_for_tablename.sql(并立即删除,因为我们不再需要它)。现在&mytable包含脚本开头的默认值,或者用户在提示时输入的内容。

已添加:包含两个变量的版本

这会将动态查询构建为:

select count(*) into vari_axu from &mytable where created > date '&busdate';

出于演示目的,您可以输入表格名称user_objects(其中created是日期列)。

显然,这种结构变得复杂且容易出错,因为用户必须指定一个具有预期列名的表,所以我不确定我是否建议在这条路径上走太远,但无论如何:

set define "&"
set verify off
define mytable = dual
define busdate = "0001-01-01"
define if_param_is_1 = "--"

accept param prompt "Enter option 1-9: "

-- Generate a script named prompt_for_tablename.sql which prompts the user for a table name
-- or does nothing, depending on the value of &param:
set termout off
column mytable new_value mytable
column if_param_is_1 new_value if_param_is_1

set head off feedback off
spool prompt_for_tablename.sql
select prompt, null as if_param_is_1  -- uncomment
from
(
  select 'accept mytable char prompt "Enter table name: "'||chr(13)||chr(10) as prompt from dual
  union all
  select 'accept busdate date format ''YYYY-MM-DD'' prompt "Enter business date (YYYY-MM-DD): "' from dual
)
where &param = 1;
spool off
set termout on head on feedback on

@prompt_for_tablename.sql
host del prompt_for_tablename.sql

declare
    vari_axu number;
begin
    &if_param_is_1 dbms_output.put_line ('work here');
    &if_param_is_1 select count(*) into vari_axu from &mytable where created > date '&busdate';
    &if_param_is_1 dbms_output.put_line('Count of &mytable created after &busdate: ' || vari_axu);
    &if_param_is_1 return;

    dbms_output.put_line ('do not work' );
end;
/

测试传递param为2:

SQL> @demo
Enter option 1-9: 2

do not work

PL/SQL procedure successfully completed.

测试传递param为1:

SQL> @demo
Enter option 1-9: 1
Enter table name: user_objects
Enter business date (YYYY-MM-DD): 2010-01-01

work here
Count of user_objects created after 2010-01-01: 93772

PL/SQL procedure successfully completed.

(您可以使用successfully completed取消set feedback off条消息。)