简体   繁体   English

在where子句SQL中的case语句中使用参数

[英]Using parameters in case statements in where clause SQL

I need to create a report by using ssrs but i'm having some difficulties on parameters. 我需要使用ssrs创建一个报告,但是我在参数上遇到了一些困难。 I have 3 parameters, Beginning Date, Ending date and SalesID. 我有3个参数,开始日期,结束日期和SalesID。

here is a list of what i need to do; 这是我需要做的事的清单;

1-Dates can be null and if left null, all dates should be on report ( i guess a case statement is necessary here) 1-日期可以为空,如果保留为空,则所有日期都应在报告中(我想这里需要一个案例说明)

2- SalesID's are entered manually and can be multiple value or null. 2-SalesID是手动输入的,可以是多个值或null。 If it's null, all salesid's should come into report. 如果为空,则所有salesid都应报告。

So far i tried something like this about SalesId's and dates, a piece of query is below; 到目前为止,我已经尝试过类似SalesId和日期的操作,下面是一个查询:

AND ST.CREATEDDATE BETWEEN (@Baslangic) and (@Bitis)
AND CASE CPDR.SALESID   WHEN @SalesID THEN @SalesID
                        WHEN NULL THEN 'Hepsi'   END

(Hepsi means "All" in my native language so it's a bit of tricking SSRS in parameter selection, not sure if it would work. Other parameters are simply "beginning" and "end" as well.) (Hepsi在我的母语中意为“全部”,因此在参数选择中有点欺骗SSRS,不确定是否可以使用。其他参数也只是“开始”和“结束”。)

I'm getting "an expression of non-boolean type specified in a context where a condition is expected" error on case statement, not sure what to do. 我在case语句上收到“在预期条件的上下文中指定的非布尔类型的表达式”错误,不确定该怎么办。

What am i doing wrong? 我究竟做错了什么?

Something like this I think 我想是这样的

   AND XXXX = CASE CPDR.SALESID   WHEN @SalesID THEN @SalesID
   WHEN NULL THEN 'Hepsi'   END

What is it that's going to be put equal to @SalesID or 'Hepsi'? 将与@SalesID或'Hepsi'相等的是什么? You seem to be missing something infront of the case statement to compare with (hence the boolean expression error). 您似乎在case语句的前面缺少要比较的内容(因此布尔表达式错误)。 I've added XXXX in my example. 我在示例中添加了XXXX。

Edit - also unless you're going to have more WHEN cases after the example, use ELSE instead of the last WHEN: 编辑-同样,除非您在示例之后有更多WHEN案例,否则请使用ELSE而不是最后一个WHEN:

ELSE 'Hepsi' END

Try smt like this: 像这样尝试smt:

AND (@Baslangic is null OR ST.CREATEDDATE BETWEEN (IsNull(@Baslangic,GETDATE())) and (isNull(@Bitis,GETDATE())))
AND (@SalesID is null OR CPDR.SALESID IN (@SalesID))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM