Golan_trevize
Golan_trevize

Reputation: 2433

IF WITH ORs conditions not runnng as expected PL/SQL

Hello I have this easy sample code not running as I expect in PL/SQL:

declare 

A NUMBER := NULL;
B NUMBER := NULL;
C NUMBER := NULL;
D NUMBER := NULL;
E NUMBER := 7;

BEGIN

IF A != NULL OR B != NULL OR C != NULL OR D != NULL OR E != NULL THEN

DBMS_OUTPUT.PUT_LINE('IF');

ELSE 

DBMS_OUTPUT.PUT_LINE('ELSE');

END IF;

END;

So as far as I know, when multiple ORs go as condition as long as one of them is true, the condition becomes true for all of them too. So in thos case E is 7, so E is != to 7, so in my mind it should enter the IF, but in the output "ELSE" comes.

Upvotes: 0

Views: 55

Answers (1)

William Robertson
William Robertson

Reputation: 16001

Your != NULL conditions are problem, because nothing is equal or not equal to null. Replace them with is not null and it works as expected:

declare
    a number := null;
    b number := null;
    c number := null;
    d number := null;
    e number := 7;
begin
    if a is not null or b is not null or c is not null or d is not null or e is not null then
        dbms_output.put_line('IF');
    else
        dbms_output.put_line('ELSE');
    end if;
end;

As an alternative, you could test coalesce(a,b,c,d,e) to get the same result.

Upvotes: 2

Related Questions