Home > Software engineering >  OR operator in if condition is not working in plsql block
OR operator in if condition is not working in plsql block

Time:06-06

declare

myex exception;
no account.ano %type;
b account.bal%type;
branch account.Bname%type;

begin
no:=:no;
b:=:b;
branch:=:branch;

if branch <> 'surat'  then
raise myex;
end if;

insert into account values(no,b,branch);
commit;
dbms_output.put_line('record inserted successfully...');

exception
when myex then
dbms_output.put_line('invalid branch name ');
end;

This code is working properly just as expected but when I try to add more branch names in the if condition using or . Its not working . The exception is raised for all the entries even the valid branch names like surat or vadodra .

declare

myex exception;
no account.ano %type;
b account.bal%type;
branch account.Bname%type;

begin
no:=:no;
b:=:b;
branch:=:branch;

if branch <> 'surat' or branch <>'vadodra' or branch <>'ahmedabad' then
raise myex;
end if;

insert into account values(no,b,branch);
commit;
dbms_output.put_line('record inserted successfully...');

exception
when myex then
dbms_output.put_line('invalid branch name ');
end;

CodePudding user response:

That's because you most probably wanted AND, not OR:

if branch <> 'surat' and branch <>'vadodra' and branch <>'ahmedabad' then

Alternatively, use IN:

if branch not in ('surat', 'vadodra', 'ahmedabad') then
  • Related