简体   繁体   English

为什么我的 null 检查在 Oracle 的这个 IF 语句中不起作用?

[英]Why is my null check not working in this IF statement in Oracle?

This should be a simple question.这应该是一个简单的问题。 I want a particular column claimedby to alternate between a user ID and nothing.我想要一个特定的列 claimby 在用户 ID 和任何内容之间交替。 So, I'm writing a simple procedure that should look at the column to see if a user ID is in there.所以,我正在编写一个简单的程序,它应该查看该列以查看用户 ID 是否在其中。 If it is, set it to nothing.如果是,请将其设置为空。 If it is nothing, then set it to the user ID.如果没有,则将其设置为用户 ID。 That's all I need.这就是我所需要的。 But, I'm running into difficulties with determining if the column is nothing.但是,我在确定该列是否为空时遇到了困难。 I'm trying various ways, such as 1) case claimedby when null, 2) case claimedby when '', 3) if length(claimedby) < 1. In none of the cases is the column being set to the incoming parameter userID.我正在尝试各种方法,例如 1) null 时的情况,2) '' 时的情况,3) 如果长度(claimedby) < 1。在任何情况下,都没有将列设置为传入参数 userID。 It's always remaining null.它始终是 null。 Here's my code:这是我的代码:

create or replace PROCEDURE claimprocedure
   ( userID IN varchar2, docketnum IN varchar2 )
IS
   claimed_by varchar2(10);

  cursor c1 is
    SELECT claimedby FROM DR_RULE24.CISR_INTAKE_ERRORS_MASTER 
     WHERE docket_num = docketnum;
     
BEGIN

  open c1;
  fetch c1 into claimed_by;
  close c1;

 DBMS_OUTPUT.PUT_LINE('claimed_by = ' || claimed_by);

CASE claimed_by
    when null then update DR_RULE24.cisr_intake_errors_master set claimedby = userID where docket_num = docketnum;
    when '' then update DR_RULE24.cisr_intake_errors_master set claimedby = userID where docket_num = docketnum;
    ELSE update DR_RULE24.cisr_intake_errors_master set claimedby = '' where docket_num = docketnum;
  END CASE;
  
  if length(claimed_by) < 1 then update DR_RULE24.cisr_intake_errors_master set claimedby = userID where docket_num = docketnum;
    else update DR_RULE24.cisr_intake_errors_master set claimedby = '' where docket_num = docketnum;
   end if;

  if claimed_by = '' then update DR_RULE24.cisr_intake_errors_master set claimedby = userID where docket_num = docketnum;
  else update DR_RULE24.cisr_intake_errors_master set claimedby = '' where docket_num = docketnum;
 end if;

if claimed_by is null then update DR_RULE24.cisr_intake_errors_master set claimedby = userID where docket_num = docketnum;
  else update DR_RULE24.cisr_intake_errors_master set claimedby = '' where docket_num = docketnum;
 end if;
   
END;

I confirm the column is set to null, then I call it with:我确认该列设置为 null,然后我调用它:

SET SERVEROUTPUT ON
call claimprocedure('shone','106712');

The column for docket number 106712 is not being set to 'shone'.案卷编号 106712 的列未设置为“闪耀”。 I've confirmed that the claimed_by variable in the procedure matches the structure of the claimedby column, which is varchar2(10);我已经确认该过程中的 claim_by 变量与 claimby 列的结构相匹配,即 varchar2(10);

I thought that maybe the IF statements might be canceling each other out.我想也许 IF 语句可能会相互抵消。 So, I commented out all the ELSE clauses and tried again.所以,我注释掉了所有的 ELSE 子句并再次尝试。 It's still null, proving that none of these IF statements are working.仍然是 null,证明这些 IF 语句都不起作用。

It must be something simple.它一定很简单。 What am I missing?我错过了什么? Thank you.谢谢你。

The case statement (and its cousin the case expression) comes in two forms: the "simple" form and the "searched" form. case语句(及其表亲case表达式)有两种 forms:“简单”形式和“搜索”形式。

The simple form looks like case <expr> when <val1> then... when <val2> then... else... end - this is simple, easy to read, but it only works for direct equality checks.简单的形式看起来像case <expr> when <val1> then... when <val2> then... else... end - 这很简单,易于阅读,但它仅适用于直接相等检查。 The searched form looks like case when <condition1> then... when <condition2> then... else... end .搜索到的表单类似于case when <condition1> then... when <condition2> then... else... end的情况。 Here you write a bit more code, but this form is much more general.在这里你写了更多的代码,但这种形式更通用。

Now to your question.现在回答你的问题。 You are using the simple form.您正在使用简单的表格。 Its meaning is equivalent to case when claimed_by = null then..... Can you spot the trouble here?它的含义相当于case when claimed_by = null then.....你能发现这里的问题吗? If you can't, read again about null , and when something ( anything ) is ever equal to null .如果不能,请再次阅读有关null的内容,并且当某些东西(任何东西等于null

You must use the searched form, and write your condition in the correct, SQL way: case when claimed_by IS null then....您必须使用搜索到的表格,并以正确的 SQL 方式写下您的条件: case when claimed_by IS null then....

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

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