Oracle PL/SQL string compare issue

前端 未结 7 1131
予麋鹿
予麋鹿 2020-12-24 14:16

I have the following Oracle PL/SQL codes that may be rusty from you guys perspective:

 DECLARE
 str1  varchar2(4000);
 str2  varchar2(4000);
 BEGIN
   str1:         


        
7条回答
  •  臣服心动
    2020-12-24 14:56

    To fix the core question, "how should I detect that these two variables don't have the same value when one of them is null?", I don't like the approach of nvl(my_column, 'some value that will never, ever, ever appear in the data and I can be absolutely sure of that') because you can't always guarantee that a value won't appear... especially with NUMBERs.

    I have used the following:

    if (str1 is null) <> (str2 is null) or str1 <> str2 then
      dbms_output.put_line('not equal');
    end if;
    

    Disclaimer: I am not an Oracle wizard and I came up with this one myself and have not seen it elsewhere, so there may be some subtle reason why it's a bad idea. But it does avoid the trap mentioned by APC, that comparing a null to something else gives neither TRUE nor FALSE but NULL. Because the clauses (str1 is null) will always return TRUE or FALSE, never null.

    (Note that PL/SQL performs short-circuit evaluation, as noted here.)

提交回复
热议问题