1

I would like to have something like this

condition := case {SOME_VAR}
  when 1 then 'tbl.id = 3 and tbl.code = 6'
  when 2 then 'tbl.id != 4 and tbl.code = 5'
  when 3 then 'tbl.id = 2 and tbl.code != 7'
  else '1 = 1'
end

select
  *
from
  some_table tbl
where
  $condition
;

and $condition will be replaced with the corresponding SQL code from above the select clause, something like a macro replacement. Is this possible in Oracle?

4

2 回答 2

0
select
  *
from
  some_table tbl
where
  SOME_VAR = 1 and tbl.id = 3 and tbl.code = 6 or
  SOME_VAR = 2 and tbl.id != 4 and tbl.code = 5 or
  SOME_VAR = 3 and tbl.id = 2 and tbl.code != 7
于 2013-06-04T16:23:10.957 回答
0

我不确定甲骨文,但SQL总的来说,是的,不是的。

你不能完全按照你的要求做,但是对于你引用的具体情况,你可以

SELECT *
FROM some_table tbl
WHERE tbl.id = CASE {some_var} WHEN 1 THEN 3
                               WHEN 2 THEN 4
                               WHEN 3 THEN 2
                               ELSE tbl.id END
AND tbl.code = CASE {some_var} WHEN 1 THEN 6
                               WHEN 2 THEN 5
                               WHEN 3 THEN 7
                               ELSE tbl.code END
于 2013-06-04T15:29:27.690 回答