【发布时间】:2020-07-24 13:59:56
【问题描述】:
我有两个这样的查询
select *
from table 1
where id=145
查询 2
select *
from table 1
where code=452 and typeCode <> 3
如何根据参数值过滤 where 条件,例如,我有以下参数,可以是“历史”或“最新”等
Declare @Type nvarchar(10)
set @Type='History'
所以现在我想在我的 where 子句中说 if type = history 而不是第一个查询如果它等于最新而不是第二个查询
select *
from table 1
where if @Type='History'
then
id=145
else if @type='latest'
then
code=452 and typeCode <> 3
或如何使用上面的案例
where case @type='History'
then
id=145
when @type='Latest'
then
code=452 and typeCode <> 3
我的语法不正确,我该如何做到这一点?
【问题讨论】:
标签: sql-server if-statement switch-statement