加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

TSQL案例,其中statement if参数为null

发布时间:2020-12-12 08:52:54 所属栏目:MsSql教程 来源:网络整理
导读:我有一个SP给我很多困难的时刻. sp得到两个参数@madeByUserId和@reportedByUserId. 我想要这样的东西: select * from table where MadeByUserId = @madeByUserId (if(@reportedByUserID != null) and ReportedByUserID = @reportedByUserID) 基本上我想在whe
我有一个SP给我很多困难的时刻.

sp得到两个参数@madeByUserId和@reportedByUserId.
我想要这样的东西:

select * from table
  where MadeByUserId = @madeByUserId (if(@reportedByUserID != null) and ReportedByUserID = @reportedByUserID)

基本上我想在where子句中添加一个case,以包含基于@reportedByUserId的null / not null状态的另一个过滤条件

那可能吗?

非常感谢,
拉杜

解决方法

尝试:
select * from table
 where MadeByUserId = @madeByUserId 
 AND (@reportedByUserID IS null OR ReportedByUserID = @reportedByUserID)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读