简体   繁体   English

检查列 IS NULL 在案例表达式中不起作用

[英]Check column IS NULL not working in Case Expression

在此处输入图像描述

I am trying to check a column which belongs to View "IS NULL" and it works good with select statement:我正在尝试检查属于视图“IS NULL”的列,它适用于 select 语句:

SELECT TOP (1000) [Invoice_Number]
  ,[Invoice_Date]
  ,[Invoice_Amount]
  ,[Invoice_CoCd]
  ,[Invoice_vendor]
  ,[Invoice_PBK]
  ,[Invoice_DType]
  ,[Invoice_DueDate]
  ,[Invoice_ClgDate] FROM [dbo].[viewABC] where   Invoice_PBK IS NULL

Now I have an update statement, where I need to update a column in a table based on NULL in VIEW:现在我有一个更新语句,我需要在 VIEW 中根据 NULL 更新表中的列:

UPDATE cis
    SET
    cis.InvoiceStatus = 
    (
    CASE 

    WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN 
    'HELLO'  
    WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' 
    THEN        '233'  END)
FROM 
    [dbo.[tblABC] cis,
    [dbo].[viewABC] imd 
WHERE [condition logic]

These is no issue with where condition, the IS NULL in the CASE expression causing the problem.这些与 where 条件无关, CASE表达式中的IS NULL会导致问题。

Can someone help please?有人可以帮忙吗?

Voting to close this as typographical, but to show the OP their typo:投票将其关闭为印刷,但向 OP 展示他们的错字:

UPDATE cis
    SET cis.InvoiceStatus = CASE WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN 'HELLO'  
                                 WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN  '233'
                            END
FROM 
    [dbo.[tblABC] cis,
    [dbo].[viewABC] imd --Stop using 30 year old syntax, it was replaced 30 years ago.
WHERE [condition logic]

And, as my comment says, stop using joins that are 27 years out of date.而且,正如我的评论所说,停止使用已经过时 27 年的联接。 Bad habits to kick: using old-style JOINs 要改掉的坏习惯:使用老式的 JOIN

Looking at this expression:看这个表达式:

cis.InvoiceStatus =
CASE 
    WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN 
        'HELLO'  
    WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
        '233'  
END

And this symptom:而这个症状:

CIS.InvoiceStatus gets updated with NULL CIS.InvoiceStatus 更新为 NULL

The obvious conclusion is neither WHEN condition is met, and therefore the result of the CASE expression is NULL .显而易见的结论是不满足WHEN条件,因此CASE表达式的NULL

Maybe you wanted this, which will preserve the original value in that situation:也许你想要这个,它会在这种情况下保留原始值:

cis.InvoiceStatus =
CASE 
    WHEN RTRIM(LTRIM(imd.[Invoice_PBK])) IS NULL THEN 
        'HELLO'  
    WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
        '233'  
    ELSE
        cis.InvoiceStatus
END

Or maybe you wanted this, to also match an empty string value:或者也许你想要这个,也匹配一个空字符串值:

cis.InvoiceStatus =
CASE 
    WHEN NULLIF(RTRIM(LTRIM(imd.[Invoice_PBK])),'') IS NULL THEN 
        'HELLO'  
    WHEN RTRIM(LTRIM(imd.Invoice_DType)) = 'RD' THEN
        '233'  
END

It's also worth pointing out the two WHEN conditions are looking at two different columns.还值得指出的是,两个WHEN条件正在查看两个不同的列。

Finally, it may be worth a data clean-up project here.最后,这里可能值得一个数据清理项目。 Needing to do an LTRIM() will break any chance of using indexes on those fields ( RTRIM() is slightly less bad), and index use cuts to the core of database performance.需要执行LTRIM()将破坏在这些字段上使用索引的任何机会( RTRIM()稍微不那么糟糕),并且索引使用会降低数据库性能的核心。

You may want to just test your Logic piece by piece.您可能只想逐个测试您的逻辑。 Try尝试

SELECT ISNULL(RTRIM(LTRIM(imd.[Invoice_PBK])),'Hey Im Null')
FROM 
    [dbo].[tblABC] cis,
    [dbo].[viewABC] imd 
WHERE [condition logic]

See if you get that String value on your returned column(s) from the Query.查看您是否从查询返回的列上获得了该字符串值。 Then, build from there.然后,从那里构建。 It could be something as simple as the JOIN .它可能像JOIN一样简单。 Which I'm not a fan of that old syntax but, without more info on this table other than [conditions] .我不是那种旧语法的粉丝,但是除了[conditions]之外,没有关于这个表的更多信息。 It's hard to just guess an answer for you.很难为你猜出答案。 You have no detailed evidence that helps us.你没有详细的证据可以帮助我们。 It very well could be your conditions but, you're saying "condition logic" and that does nothing for the group on this thread.这很可能是您的条件,但是您说的是“条件逻辑”,这对该线程上的组没有任何作用。

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

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