gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:06:43 34 4
gpt4 key购买 nike

我有一个给我带来很多困难的 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com