如何把下面的语句转化成where ...case结构,

brotherfromaq 2005-09-01 01:55:55
declare @test table (context nvarchar(10),type nvarchar(10))
declare @type nvarchar(10)

insert @test values ('1','CRP')
insert @test values ('2','CRP')
insert @test values ('3','CRP')
insert @test values ('4','CRP')
insert @test values ('5','CRP')
insert @test values ('5','CRPT')
insert @test values ('6','CRPT')
insert @test values ('7','CRPT')
insert @test values ('1',NULL)
insert @test values ('1',NULL)
insert @test values ('7','BTO')
insert @test values ('7','BTO')
insert @test values ('7','BTO')
if @type='CRP'
begin
select * from @test where type like '%'+@type+'%' OR type is null
end
else
begin
select * from @test where type like '%'+@type+'%'
end
...全文
101 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
brotherfromaq 2005-09-01
  • 打赏
  • 举报
回复
vivianfdlpw:run 出来的结果不对。
vivianfdlpw 2005-09-01
  • 打赏
  • 举报
回复
select *
from @test
where type like '%'+@type+'%'
or
nullif(@type,'CRP') is null
zjcxc 元老 2005-09-01
  • 打赏
  • 举报
回复
--没有 where case 结构,只有case when

--可以改为:

select * from @test
where (@type='CRP' and (type like '%'+@type+'%' OR type is null))
or (@type<>'CRP' and (type like '%'+@type+'%'))

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧