我有这样的东西:
declare @cucu varchar(10)
select @cucu = 'c'
create table #t(id int, title varchar(50), p bit)
insert into #t(id, title, p) values (1, 'alpha', 0)
insert into #t(id, title, p) values (2, 'beta', 1)
insert into #t(id, title, p) values (3, 'gamma', 0)
if (@cucu = 'a')
begin
select *
from #t
where p = 0
end
else if (@cucu = 'b')
begin
select *
from #t
where p = 1
end
else
begin
select *
from #t
end
drop table #t
有没有办法在select * from #t
个查询的第WHERE
个问题上以CASE/WHEN/END
的形式处理这3个病例?
如果我创建另一个变量,如下所示:
declare @b bit; select @b = 0; if @cucu = 'b' select @b = 1
那么在一个查询中处理前两个 case 就很简单了:
select *
from #t
where p = @b
但对于第三种可能性,我不知道该怎么做