SQL中的宏替换

时间:2013-06-04 15:23:59

标签: sql oracle macros

我想有类似的东西

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
;

$condition将替换为select子句上方的相应SQL代码,类似于宏替换。这可能在Oracle吗?

2 个答案:

答案 0 :(得分:0)

我特别不确定Oracle,但总体来说是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

答案 1 :(得分: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