Thursday, January 09, 2014

Operator Precedence in PL/SQL

This was tested on Oracle 11.2. Many platforms give unary plus and minus a higher priority than exponentiation but PL/SQL does not:

SQL> SET SERVEROUTPUT ON
SQL> DECLARE
  2   A NUMBER := -2**2;
  3  BEGIN
  4   DBMS_OUTPUT.PUT_LINE('A = '||A);
  5  END;
  6  /
A = -4
 
PL/SQL procedure successfully completed.
 
SQL>
 
If you do not like this, you can change it with parentheses:
 
SQL> DECLARE
  2   B NUMBER := (-2)**2;
  3  BEGIN
  4   DBMS_OUTPUT.PUT_LINE('B = '||B);
  5  END;
  6  /
B = 4
 
PL/SQL procedure successfully completed.
 
SQL>

No comments:

Post a Comment