Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OR operator in if condition is not working in plsql block

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;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading