SQL中的宏替换
我想要这样的SQL中的宏替换,sql,oracle,macros,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 ; 而$con
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中是可能的吗?我不确定Oracle的具体情况,但在SQL
中,一般来说是和否
你不能完全按照你想要的做,但是对于你引用的具体案例,你可以这样做
您可以在生成时或运行时运行一些动态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
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