Solved

pl/sql exclusive OR

Posted on 2007-11-15
5
5,492 Views
Last Modified: 2013-12-07
How do you code an exclusive OR in PL SQL.
ie: one or other but not both. Is there an operator to do this?
ie: if both are true then return false
0
Comment
Question by:claghorn
5 Comments
 
LVL 1

Expert Comment

by:bobbymanocha
ID: 20289580
Here's one way:

create or replace function f_xor(
  condition1 boolean,
  condition2 boolean) return boolean
as
begin
  return (condition1 and condition2);
end;
/
0
 
LVL 1

Expert Comment

by:bobbymanocha
ID: 20289649
I posted the wrong code earlier.  Sorry about that.

create or replace function f
(condition1 boolean, condition2 boolean) return boolean as  
begin
return XOR(condition1, condition2);
end;
/
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 20296637
another sample


DECLARE

  a BOOLEAN;

  b BOOLEAN;
 

  

  FUNCTION exclusive_or(c1 IN BOOLEAN, c2 IN BOOLEAN)

  RETURN BOOLEAN

  IS 

  BEGIN

    RETURN ((c1 OR c2) AND NOT (c1 AND c2));

  END;

  

  

BEGIN

  

  a := TRUE;

  b := TRUE;

    

  IF exclusive_or(a,b) THEN

    DBMS_OUTPUT.PUT_LINE('Both TRUE results in TRUE');

  ELSE

    DBMS_OUTPUT.PUT_LINE('Both TRUE results in FALSE');

  END IF;

  

  a := FALSE;

  b := TRUE;

    

  IF exclusive_or(a,b) THEN

    DBMS_OUTPUT.PUT_LINE('First FALSE, second TRUE results in TRUE');

  ELSE

    DBMS_OUTPUT.PUT_LINE('First FALSE, second TRUE results in FALSE');

  END IF;
 

  a := TRUE;

  b := FALSE;

    

  IF exclusive_or(a,b) THEN

    DBMS_OUTPUT.PUT_LINE('First TRUE, second FALSE results in TRUE');

  ELSE

    DBMS_OUTPUT.PUT_LINE('First TRUE, second FALSE results in FALSE');

  END IF;
 

  a := FALSE;

  b := FALSE;

    

  IF exclusive_or(a,b) THEN

    DBMS_OUTPUT.PUT_LINE('Both FALSE results in TRUE');

  ELSE

    DBMS_OUTPUT.PUT_LINE('Both FALSE results in FALSE');

  END IF;
 

END;  

  

Open in new window

0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 20297887
( c1 and not c2 ) or ( c2 and not c1 )  --> i think this should be fine as well.

Thanks
0
 

Author Comment

by:claghorn
ID: 20298518
OK, thank you all, I will assess which one works for me tonight and release the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now