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)?
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 ).
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?
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.
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.
Yes. PL/SQL performs short circuit evaluation of logical expressions from left to right.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With