【发布时间】:2019-08-08 09:59:13
【问题描述】:
如果变量 1 为空 从表中选择 * 条件1 和条件 2 并且 a 不为空且 b 不为空 别的 从表中选择 * 条件1 和条件 2 a 为空,b 为空
由于“if and else”中只有最后两个条件发生变化,如何将其编写为动态 SQL 查询?
【问题讨论】:
-
想知道你在处理什么数据库管理器???
如果变量 1 为空 从表中选择 * 条件1 和条件 2 并且 a 不为空且 b 不为空 别的 从表中选择 * 条件1 和条件 2 a 为空,b 为空
由于“if and else”中只有最后两个条件发生变化,如何将其编写为动态 SQL 查询?
【问题讨论】:
这可能有效,也可能无效,具体取决于正在使用的数据库。试一试。
Select * from ConditionalWhere
Where condition1 = 1
And condition2 = 1
AND CASE When (variable1 is null)
And ([a] is not null)
and ([b] is not null)
Then 1
When (variable1 is not null)
and ([a] is null)
and ([b] is null)
Then 1
Else 0
End = 1
【讨论】: