sql - TSQL where 语句中如果参数为空的情况

标签 sql sql-server tsql

我有一个给我带来很多困难的 SP。

sp 获取两个参数@madeByUserId 和@reportedByUserId。 我想要这样的东西:

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

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

这可能吗?

非常感谢, 拉杜

最佳答案

尝试:

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

关于sql - TSQL where 语句中如果参数为空的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4385389/

相关文章:

sql - 平均模式 - 在 SQL Server 中需要帮助进行计算

sql - 陷入 t-SQL 中单个表上的选择性 DELETE 语句

sql - 如何使用sql表值参数来过滤结果?

sql - SSRS 性能 - 过滤属性与 WHERE 条件

SQL Server - 表变量与带有 union 的 select 语句的临时表

asp.net - 下拉列表值和文本需要修剪

sql-server - 在 SQL Server 中对两列执行双重排序,并且主排序不在前两列的计算中

mysql - SQL 查询左连接问题

mysql - 如何在mysql中显示具有多个音乐流派类别的专辑?

tsql - FREETEXTTABLE 的等级始终为 0