我无法找到这个看似简单问题的答案。我需要根据条件执行不同的SELECT语句。
一个简单的例子:
if <condition>
SELECT id FROM table;
otherwise
SELECT id FROM table WHERE variable=1;
换句话说,我需要根据条件添加一个额外的WHERE子句,但问题归结为根据条件是真还是假来选择不同的SELECT子句。
答案 0 :(得分:1)
SELECT id FROM table WHERE variable=1 OR <condition>
答案 1 :(得分:0)
案例陈述......
SELECT id FROM table
where case when variable is not null then variable else 1 end
=
case when variable is not null then @variable else 1 end
你可能想要在case语句中使用不同的逻辑,但这就是说当变量有一个值(非null)然后使用where子句变量= @variable,否则1 = 1(这会带回所有行/ 没有过滤器)。