Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL Evaluation Order

Tags:

oracle

plsql

Howdy. Consider the following:

SQL> DECLARE
  2     b1   BOOLEAN;
  3     b2   BOOLEAN;
  4     FUNCTION checkit RETURN BOOLEAN IS
  5     BEGIN
  6        dbms_output.put_line('inside checkit');
  7        RETURN TRUE;
  8     END checkit;
  9  
 10     PROCEDURE outp(n VARCHAR2, p BOOLEAN) IS
 11     BEGIN
 12        IF p THEN
 13           dbms_output.put_line(n||' is true');
 14        ELSE
 15           dbms_output.put_line(n||' is false');
 16        END IF;
 17     END;
 18  BEGIN
 19     b1 := TRUE OR checkit;
 20     outp('b1',b1);
 21     b2 := checkit OR TRUE;
 22     outp('b2',b2);
 23  END;
 24  /

b1 is true
inside checkit
b2 is true

PL/SQL procedure successfully completed

SQL> 

Notice that the results of the OR statements are order dependent. If I place the function call first, then the function is executed regardless of the value of the other term. It appears that an OR statement is evaluated left to right until a TRUE is obtained, at which point processing stops and the result it TRUE.

My question is, is this something I can rely on? Or could this behavior change in future releases of PL/SQL? If it could change, is there a way to force the function to be evaluated that I can rely on (without creating another variable and using a separate assignment statement)?

like image 739
DCookie Avatar asked Oct 09 '08 16:10

DCookie


People also ask

In which order does the Oracle server evaluate clauses?

The order in which clauses are logically processed by Oracle is as follows: FROM -> CONNECT BY -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY . Of course, your query does not have to have every clause, and some cannot even be used with/without others (e.g. HAVING can only be used when you use GROUP BY ).

What is the order of execution of SQL query in Oracle?

When a query is submitted to the database, it is executed in the following order: FROM clause WHERE clause GROUP BY clause HAVING clause SELECT clause ORDER BY clause So why is it important to understand this?

Does order of WHERE clause matter in SQL?

In SQL Server order does not matter in the WHERE condition. SQL Server does not short circuit conditions as well it does not help in performance.

Is PL SQL better than SQL?

Q: Which is better: SQL or PL/SQL? PL/SQL is an extension of SQL and further includes many procedural features like function, data variables, exception handling, and triggers. Moreover, PL/SQL allows us to transfer an entire block of statements to the database at once whereas SQL executes a single query at a time.


2 Answers

Yes. PL/SQL performs short circuit evaluation of logical expressions from left to right.

like image 90
Justin Cave Avatar answered Oct 14 '22 09:10

Justin Cave


If it could change, is there a way to force the function to be evaluated that I can rely on (without creating another variable and using a separate assignment statement)?

If you require that the function must be evaulated even when it is logically superfluous to do so, that implies that it does something other than simply return TRUE or FALSE, e.g. perhaps it updates a table. It isn't considered good practice for PL/SQL functions to have such "side effects".

like image 31
Tony Andrews Avatar answered Oct 14 '22 11:10

Tony Andrews