我需要做的是检查过程中的一系列变量是否有值,但棘手的部分是我必须检查哪些变量的列表在表中。让我进一步解释一下:
我们有一个表,其中保存所有变量名称以及哪个变量不应为null的指示符。这样我们就可以在该表中更改哪些字段是必需的而无需更改代码。
我们要实现的内容与表单中内置的NAME_IN类似,您可以使用v_value := GetValue('v_variable');
这样的内容,这样可以确定v_variable
对v_value
的价值。然后我会检查v_value
是否为空。整个事情都在一个游标的循环中,它将获得我前面提到的表中被标记为必需的所有变量。
到目前为止,我已尝试使用EXECUTE IMMEDIATE来动态分配变量值,但这不起作用,因为EXECUTE IMMEDIATE在其自己的范围内运行,因此无法“查看”过程范围中的变量。
我尝试过的另一件事是PL / SCOPE,它允许我通过提供名称来实际查看变量是否存在于我的范围内,但它没有机制来获取存在的变量的值。
好吧,我希望有人能帮助我。非常感谢帮助。
以下是一个例子:
假设我有两个名为tblConfig的表,其中包含两列:variable_name和required_ind。
variable_name | required_ind
-----------------------------
var1 | Y
var2 | N
var3 | N
然后我会有一个名为check_variables的过程,如下所示:
PROCEDURE check_variables (
var1 VARCHAR2,
var2 VARCHAR2,
var3 VARCHAR2)
IS
CURSOR c_var_check IS
SELECT variable_name
FROM tblConfig
WHERE required_ind = 'Y';
BEGIN
FOR rec_var IN c_var_check LOOP
... check if rec_var.variable_name is the name of variable that has value ...
END LOOP;
END;
在这个fisrt场景中,循环必须检查var1
是否有值。如果我更改了其他变量的required_ind值,也会检查它们。
我已经阅读了关于软编码的文章...这是一个很好的阅读,不幸的是在这种情况下我不是作为开发人员做出的选择。这是必要的,因为具有所需配置的表由用户管理,而不是由开发团队管理。
答案 0 :(得分:5)
PL / SQL没有太多的反思方式。当然没有NAME_IN的等价物。我用动态SQL无法解决这个问题,但我找到了解决方案。
这是一个箴言。它有三个程序。请注意,它们都是必需的,但我们可以在参数的槽中传递NULL。这当然是我对这种“软编码”的反对之一:它混淆了API。描述一个过程不再足以知道它需要什么参数。
create or replace procedure do_something
(p1 in varchar2
, p2 in varchar2
, p3 in varchar2)
is
args sys.dbms_debug_vc2coll;
begin
args := new sys.dbms_debug_vc2coll(p1, p2, p3);
for r in ( select s.varname, a.position
from syscfg s
join user_arguments a
on (s.procname = a.object_name
and s.varname = a.argument_name)
where s.procname = 'DO_SOMETHING'
and s.mandatory = 'Y'
order by a.position
)
loop
if args(r.position) is null
then
raise_application_error(-20000, r.varname ||' cannot be null');
end if;
end loop;
dbms_output.put_line('Procedure executed successfully!');
end;
/
“动态”参数检查的工作原理是使用签名顺序中的参数填充集合。我们通过将数据字典视图与配置表连接来获取已配置参数的位置。然后我们使用position作为数组的索引。请注意,该集合采用字符串。我将所有参数声明为Varchars,但您可能需要投射日期或数字。
所以,是的,它很笨重,但是“this quest of avoidance often leads towards [...] complication, convolution, and all-around unmaintainable code.”:)
以下是配置表的内容:
SQL> select * from syscfg
2 /
PROCNAME VARNAME M
------------------------------ ------------------------------ -
DO_SOMETHING P1 Y
DO_SOMETHING P2 Y
DO_SOMETHING P3 N
SQL>
所以,让我们滚!
SQL> set serveroutput on
SQL> exec do_something('A', 'Y', null)
Procedure executed successfully!
PL/SQL procedure successfully completed.
SQL> exec do_something('A', null, 'X')
BEGIN do_something('A', null, 'X'); END;
*
ERROR at line 1:
ORA-20000: P2 cannot be null
ORA-06512: at "APC.DO_SOMETHING", line 24
ORA-06512: at line 1
SQL>
看起来不错,但为了证明我的袖子里什么都没有......
SQL> update syscfg
set mandatory = 'N'
where varname = 'P2'
/
2 3 4
1 row updated.
SQL> select * from syscfg
2 /
PROCNAME VARNAME M
------------------------------ ------------------------------ -
DO_SOMETHING P1 Y
DO_SOMETHING P2 N
DO_SOMETHING P3 N
SQL> exec do_something('A', null, 'X')
Procedure executed successfully!
PL/SQL procedure successfully completed.
SQL>
也许你的客户足够坚定,认为这种超灵活性在其他地方会很方便。好消息是,这个解决方案很容易被提取到一个独立的过程中,该过程将PROCNAME和数组作为参数。
答案 1 :(得分:0)
答案 2 :(得分:0)
这与APC的answer基本相同。我刚做了以下修改:
create table syscfg (
procname varchar2(30) not null,
varname varchar2(30) not null,
mandatory varchar2(1) not null
);
insert all
into syscfg values('DO_SOMETHING', 'P_1', 'Y')
into syscfg values('DO_SOMETHING', 'P_2', 'Y')
into syscfg values('DO_SOMETHING', 'P_3', 'N')
into syscfg values('DO_SOMETHING_TWO', 'P_1', 'Y')
into syscfg values('DO_SOMETHING_TWO', 'P_2', 'Y')
into syscfg values('DO_SOMETHING_TWO', 'P_3', 'N')
into syscfg values('DO_SOMETHING_TWO', 'P_4', 'N')
into syscfg values('DO_SOMETHING_TWO', 'P_5', 'N')
select 1 from dual;
col procname for a20
col varname for a5
col mandatory for a1
select * from syscfg;
/* Supports only up to 5 parameters. */
create or replace procedure is_missing_mandatory_args (
p_procname in varchar2,
p_1 in varchar2 default null,
p_2 in varchar2 default null,
p_3 in varchar2 default null,
p_4 in varchar2 default null,
p_5 in varchar2 default null
) as
args constant sys.dbms_debug_vc2coll :=
sys.dbms_debug_vc2coll(p_1, p_2, p_3, p_4, p_5);
begin
for r in ( select s.varname, a.position
from syscfg s
join user_arguments a
on ( s.procname = a.object_name
and s.varname = a.argument_name)
where s.procname = upper(p_procname)
and s.mandatory = 'Y'
order by a.position
)
loop
if args(r.position) is null then
raise_application_error(-20000, upper(p_procname) || '.' || r.varname
||' cannot be null');
end if;
end loop;
end;
/
show errors
create or replace procedure do_something (
p_1 in varchar2 default null,
p_2 in varchar2 default null,
p_3 in varchar2 default null
) as
begin
is_missing_mandatory_args('do_something', p_1, p_2, p_3);
/* The real work takes place here. */
dbms_output.put_line('do_something() executed successfully !');
end;
/
show errors
create or replace procedure do_something_two (
p_1 in varchar2 default null,
p_2 in varchar2 default null,
p_3 in varchar2 default null,
p_4 in varchar2 default null,
p_5 in varchar2 default null
) as
begin
is_missing_mandatory_args('do_something_two', p_1, p_2, p_3, p_4, p_5);
/* The real work takes place here. */
dbms_output.put_line('do_something_two() executed successfully !');
end;
/
show errors
SQL> exec do_something(p_1 => 'foo', p_2 => 'foo');
do_something() executed successfully !
PL/SQL procedure successfully completed.
SQL> exec do_something(p_2 => 'foo');
BEGIN do_something(p_2 => 'foo'); END;
*
ERROR at line 1:
ORA-20000: DO_SOMETHING.P_1 cannot be null
ORA-06512: at "JANI.IS_MISSING_MANDATORY_ARGS", line 23
ORA-06512: at "JANI.DO_SOMETHING", line 7
ORA-06512: at line 1
SQL> exec do_something(p_1 => 'foo');
BEGIN do_something(p_1 => 'foo'); END;
*
ERROR at line 1:
ORA-20000: DO_SOMETHING.P_2 cannot be null
ORA-06512: at "JANI.IS_MISSING_MANDATORY_ARGS", line 23
ORA-06512: at "JANI.DO_SOMETHING", line 7
ORA-06512: at line 1
SQL> exec do_something_two(p_2 => 'baz', p_1 => 'buz', p_5 => 'boz');
do_something_two() executed successfully !
PL/SQL procedure successfully completed.
SQL> exec do_something_two(p_1 => 'baz');
BEGIN do_something_two(p_1 => 'baz'); END;
*
ERROR at line 1:
ORA-20000: DO_SOMETHING_TWO.P_2 cannot be null
ORA-06512: at "JANI.IS_MISSING_MANDATORY_ARGS", line 23
ORA-06512: at "JANI.DO_SOMETHING_TWO", line 9
ORA-06512: at line 1
SQL>