问题1:左右值为null使用等号进行比较的情况
-- Created on 2021/03/16 by kuroisheep
declare
i integer;
j integer;
begin
if i = j then
dbms_output.put_line('相等');
else
dbms_output.put_line('不相等');
end if;
end;
期望结果:相等
实际执行结果:不相等
问题2:左边或者右边值为null使用不等号进行比较的情况
-- Created on 2021/03/16 by kuroisheep
declare
i integer := 123;
j integer;
begin
if i <> j then
dbms_output.put_line('不相等');
else
dbms_output.put_line('相等');
end if;
end;
期望结果:不相等
实际执行结果:相等
解决方案
-- Created on 2021/03/16 by kuroisheep
declare
i integer;
j integer := 123;
begin
if i is null and j is null then
dbms_output.put_line('相等');
elsif i is null or j is null then
dbms_output.put_line('不相等');
elsif i = j then
dbms_output.put_line('相等');
else
dbms_output.put_line('不相等');
end if;
end;
期望结果:不相等
实际执行结果:不相等
总结
具体判断还是需要根据实际情况来进行,例如当参数不允许为空时,先判断参数为空直接return,然后再进行后续比较。