我已经尝试了下面的脚本来检查一个条件并产生一个布尔结果。但是,如果我提供任何数学函数它不起作用,它不会显示值。
declare
l_sql varchar2(4000);
l_condition varchar2(4000);
ignore pls_integer;
l_cursor number;
l_names dbms_sql.varchar2_table;
begin
for i in 1..2 loop
if i = 1 then
l_condition := 'sysdate > to_date(''1/1/2007'',''mm/dd/yyyy'')';
else
l_condition := ':P1_ITEM = ' 'foo' '';
end if;
l_cursor := dbms_sql.open_cursor;
l_sql := 'begin wwv_flow.g_boolean := '||l_condition||'; end;';
l_names := wwv_flow_utilities.get_binds(l_sql);
dbms_sql.parse(l_cursor,l_sql,dbms_sql.NATIVE);
for i in 1 .. l_names.count loop
dbms_sql.bind_variable( l_cursor, l_names(i), v( substr(l_names(i),2) ), 32000 );
end loop;
ignore := dbms_sql.execute(l_cursor);
dbms_sql.close_cursor( l_cursor );
if wwv_flow.g_boolean then
dbms_output.put_line(l_condition||' is true.');
else
dbms_output.put_line(l_condition||' is false.');
end if;
end loop;
end;
我正在尝试使用l_condition:= (5 > 3 AND 40 > 50) OR 10
进行评估。为什么不起作用?
答案 0 :(得分:0)
在评论中,您已经说过要尝试解决数学值(5> 3和40> 50)或10"但这不是数学函数或有效的布尔逻辑。如果您实际进行了评估,那么5 > 3
为真,40 > 50
为假,并且将这些组合在一起是有效的;但是你要尝试将其结果与数字10进行或运算,而不是另一个布尔表达式。你正在做的事情:
(TRUE AND FALSE) OR 10
如果将其插入代码中,则会引发异常;您已将自己的作业显示为:
l_condition:= (5 > 3 AND 40 > 50) OR 10
这将为您提供块中该分配的简单PLS-00382: expression is of wrong type
。如果你引用它,你就会从动态SQL中获得相同的错误(删除不相关的绑定):
create package wwv_flow as
g_boolean boolean;
end wwv_flow;
/
declare
l_sql varchar2(4000);
l_condition varchar2(4000);
ignore pls_integer;
l_cursor number;
l_names dbms_sql.varchar2_table;
g_boolean boolean;
begin
for i in 1..2 loop
if i = 1 then
l_condition := 'sysdate > to_date(''1/1/2007'',''mm/dd/yyyy'')';
else
-- l_condition := ':P1_ITEM = ' 'foo' '';
l_condition:= '(5 > 3 AND 40 > 50) OR 10';
end if;
l_cursor := dbms_sql.open_cursor;
l_sql := 'begin wwv_flow.g_boolean := '||l_condition||'; end;';
-- l_names := wwv_flow_utilities.get_binds(l_sql);
dbms_sql.parse(l_cursor,l_sql,dbms_sql.NATIVE);
-- for i in 1 .. l_names.count loop
-- dbms_sql.bind_variable( l_cursor, l_names(i), v( substr(l_names(i),2) ), 32000 );
-- end loop;
ignore := dbms_sql.execute(l_cursor);
dbms_sql.close_cursor( l_cursor );
if wwv_flow.g_boolean then
dbms_output.put_line(l_condition||' is true.');
else
dbms_output.put_line(l_condition||' is false.');
end if;
end loop;
end;
/
ORA-06550: line 1, column 52:
PLS-00382: expression is of wrong type
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
ORA-06512: at "SYS.DBMS_SQL", line 1199
ORA-06512: at line 20
如果您删除了“OR 10”部分,那么它可以正常工作:
declare
...
l_condition:= '(5 > 3 AND 40 > 50)';
...
end;
/
PL/SQL procedure successfully completed.
sysdate > to_date('1/1/2007','mm/dd/yyyy') is true.
(5 > 3 AND 40 > 50) is false.
所以问题不在于您的代码不起作用,而是您的条件无效。如果你从另一个阻止异常的块中运行它(例如when others
异常处理程序),或者你的客户端或其他任何调用它的人都没有报告异常,你需要找出原因并修复它