use*_*578 2 oracle null plsql compare
下面是我的代码。
一个变量没有价值。另一个变量是有一个值。在下面的代码中,我想打印即使在var1没有任何值的情况下变量也不相同。我怎样才能做到这一点?
CREATE OR REPLACE PACKAGE BODY mypackagebody IS
PROCEDURE comparenull() IS
l_var1 mytable.mycolumn1%TYPE;
l_var2 mytable.mycolumn2%TYPE;
BEGIN
BEGIN
SELECT var1
,var2
INTO l_var1
,l_var2
FROM mytable;
EXCEPTION
WHEN no_data_found THEN
var1 := NULL;
var2 := NULL;
END;
/* At this point var1 is NOT having any value and var2 is having a value.*/
/* The below if condition is returing false. But, I wanted to go inside the if condition and print that the var values are not same*/
IF var1 <> var2
THEN
dbms_ouput.put_line('var1 and var2 are not same');
END IF;
END comparenull;
END mypackagebody;
Run Code Online (Sandbox Code Playgroud)
我想你想要一个安全的NULL比较。在 Oracle 中,您可以使用多个条件:
IF var1 <> var2 OR
(var1 is null and var2 is not null) OR
(var1 is not null and var2 is null)
Run Code Online (Sandbox Code Playgroud)
小智 5
处理这个问题最简洁的方法可能是使用 NVL
if NVL(var1, 'NULL') <> NVL(var2, 'NULL')
Run Code Online (Sandbox Code Playgroud)
NVL 将评估变量,如果它为 null,则在比较中使用该字符串,而不是该字符串可以是您想要的任何字符串,它不必是文字字符串 NULL 我只是发现这很有用。