plsql条件where子句

car*_*ose 3 sql oracle where

我想where在sql语句中执行条件,并使用两个不同的条件,例如在伪代码中:

procedure(..., bool_value IN boolean default false) is
....
begin

select * from mytable mt
where 
     if bool_value = true then mt.criterion_1 = value
     else
        mt_.criterion_2 = value; -- if boolean value is set, use criterion_1 column, otherwise use criterion_2 column

end
Run Code Online (Sandbox Code Playgroud)

假设它是可能的,最好的方法是什么?

谢谢

shr*_*t18 6

试试这个:

bool_value_string varchar2(5)

bool_value_string = case when bool_value then 'true' else 'false' end;

select * from mytable mt
where 
(bool_value_string = 'true' and mt.criterion_1 = value)
or
(bool_value_string = 'false' and mt.criterion_2 = value)
Run Code Online (Sandbox Code Playgroud)

基本上,将你的...然后成语转换为......或者一个.布尔字段是非空和真,意味着过滤器必须是第一个标准,或者它不是,意味着过滤第二个标准.

  • 除非最近在Oracle中发生了某些变化,否则`bool_value = true`不起作用; 你不能在SQL语句中使用布尔变量. (4认同)
  • @ammoQ 是的,非常感谢您指出这一点。现在我知道了,将尽可能远离 Oracle。 (2认同)