简体   繁体   English

案例何时检查是否为空

[英]Case When to check if is null

I have a filter for the clients for a report, if the person put the filter Client it uses,else it ignores and bring all clients. 我为报告的客户提供了一个过滤器,如果有人将其使用的过滤器放在“客户”中,则它会忽略并带来所有客户。 This is my Where clause. 这是我的Where子句。

Where [S].ano_mes = @ano_mes OR @ano_mes IS NULL    
            AND S.cnpj = @cnpj or @cnpj is null     
            AND S.cod_produto = @cod_produto or @cod_produto is null    
            AND CASE WHEN (len(@cnpj) > 0) THEN 
                 (S.CGC_Cliente +''-''+S.Seq_Cliente) in(SELECT cnpjseq FROM #Tb_CliSelecionados)
                    END

The problem is in the CASE WHEN part,the rest works. 问题出在CASE WHEN部分,其余部分起作用。

If the variable @cnpj has value THEN it should use the filter. 如果变量@cnpj的值为THEN,则应使用过滤器。

The filters is CGC_CLIENTE and SEQ_CLIENTE together. 过滤器是CGC_CLIENTE和SEQ_CLIENTE一起。

The table #Tb_CliSelecionados has the parameters Concatenated equals,exemple,both the table with alias S and #Tb_CliSelecionados has '060746948041730-00' 表#Tb_CliSelecionados具有参数Concatenated等于,例如,别名为S的表和#Tb_CliSelecionados具有'060746948041730-00'

i'm getting erro in two parts in the "IN" its says incorrect syntax near "IN" And in the END saying incorrect syntax near "END".Expection ")" 我在“ IN”中分两部分得到错误提示,在“ IN”附近说不正确的语法,在END中说“ END”附近的不正确的语法。Expection“)”

Someone knows what i'm doing wrong in the case when part? 有人知道我在什么情况下做错了吗?

CASE return one expresion isn't a flow control. CASE返回一个表达式不是流控制。

 AND CASE WHEN len(@cnpj) IS NULL 
               THEN 1
          WHEN (len(@cnpj) > 0) 
               THEN  CASE WHEN exists
                           (SELECT 1 
                            FROM #Tb_CliSelecionados
                            WHERE cnpjseq = S.CGC_Cliente +''-''+ S.Seq_Cliente )
                          THEN 1 -- if exist the match return the row
                          ELSE 0 -- other wise ignore the row
                     END
         ELSE 1 -- if len(@cnpj) == 0 then return all rows
     END = 1  -- HERE YOU return the row if CASE return 1

The CASE WHEN clause is incorrect. CASE WHEN子句不正确。 It should look like: 它应该看起来像:

         AND CASE 
             WHEN @cnpj IS NULL
                 THEN 1
             WHEN (len(@cnpj) = 0) 
                 THEN 1
             WHEN (S.CGC_Cliente +''-''+S.Seq_Cliente) in (SELECT cnpjseq FROM #Tb_CliSelecionados
                 THEN 1
             ELSE 0
         END = 1

You can change that part to 您可以将该部分更改为

AND ( len(@cnpj) > 0
       AND S.CGC_Cliente + '-' + S.Seq_Cliente in (SELECT cnpjseq FROM #Tb_CliSelecionados))

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

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