Plsql - OperatorsAn operator is a symbol that tells the compiler to perform specific mathematical or logical manipulation. PL/SQL language is rich in built-in operators and provides the following types of operators:
Arithmetic Operators Following table shows all the arithmetic operators supported by PL/SQL. Assume variable A holds 10 and variable B holds 5, then:
BEGIN
dbms_output.put_line( 10 + 5); dbms_output.put_line( 10 - 5); dbms_output.put_line( 10 * 5); dbms_output.put_line( 10 / 5); dbms_output.put_line( 10 ** 5); END; / When the above code is executed at SQL prompt, it produces the following result:
15
5 50 2 100000 PL/SQL procedure successfully completed. Relational Operators Relational operators compare two expressions or values and return a Boolean result. Following table shows all the relational operators supported by PL/SQL. Assume variable A holds 10 and variable B holds 20, then:
DECLARE
a number (2) := 21; b number (2) := 10; BEGIN IF (a = b) then dbms_output.put_line('Line 1 - a is equal to b'); ELSE dbms_output.put_line('Line 1 - a is not equal to b'); END IF; IF (a < b) then dbms_output.put_line('Line 2 - a is less than b'); ELSE dbms_output.put_line('Line 2 - a is not less than b'); END IF; IF ( a > b ) THEN dbms_output.put_line('Line 3 - a is greater than b'); ELSE dbms_output.put_line('Line 3 - a is not greater than b'); END IF; -- Lets change value of a and b a := 5; b := 20; IF ( a <= b ) THEN dbms_output.put_line('Line 4 - a is either equal or less than b'); END IF; IF ( b >= a ) THEN dbms_output.put_line('Line 5 - b is either equal or greater than a'); END IF; IF ( a <> b ) THEN dbms_output.put_line('Line 6 - a is not equal to b'); ELSE dbms_output.put_line('Line 6 - a is equal to b'); END IF; END; / When the above code is executed at SQL prompt, it produces the following result:
Line 1 - a is not equal to b
Line 2 - a is not less than b Line 3 - a is greater than b Line 4 - a is either equal or less than b Line 5 - b is either equal or greater than a Line 6 - a is not equal to b PL/SQL procedure successfully completed Comparison Operators Comparison operators are used for comparing one expression to another. The result is always either TRUE, FALSE OR NULL.
This program tests the LIKE operator, though you will learn how to write procedure in PL/SQL, but I'm going to use a small procedure() to show the functionality of LIKE operator:
DECLARE
PROCEDURE compare (value varchar2, pattern varchar2 ) is BEGIN IF value LIKE pattern THEN dbms_output.put_line ('True'); ELSE dbms_output.put_line ('False'); END IF; END; BEGIN compare('Zara Ali', 'Z%A_i'); compare('Nuha Ali', 'Z%A_i'); END; / When the above code is executed at SQL prompt, it produces the following result:
True
False PL/SQL procedure successfully completed. BETWEEN Operator: The following program shows the usage of the BETWEEN operator:
DECLARE
x number(2) := 10; BEGIN IF (x between 5 and 20) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False') ; END IF; IF (x BETWEEN 5 AND 10) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False') ; END IF; IF (x BETWEEN 11 AND 20) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False') ; END IF; END; / When the above code is executed at SQL prompt, it produces the following result:
True
True False PL/SQL procedure successfully completed. IN and IS NULL Operators: The following program shows the usage of IN and IS NULL operators:
DECLARE
letter varchar2(1) := 'm'; BEGIN IF (letter in ('a', 'b', 'c')) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False') ; END IF; IF (letter in ('m', 'n', 'o')) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False') ; END IF; IF (letter is null) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False') ; END IF; END; / When the above code is executed at SQL prompt, it produces the following result:
False
True False PL/SQL procedure successfully completed. Logical Operators Following table shows the Logical operators supported by PL/SQL. All these operators work on Boolean operands and produces Boolean results. Assume variable A holds true and variable B holds false, then:
DECLARE
a boolean := true; b boolean := false; BEGIN IF (a AND b) THEN dbms_output.put_line('Line 1 - Condition is true'); END IF; IF (a OR b) THEN dbms_output.put_line('Line 2 - Condition is true'); END IF; IF (NOT a) THEN dbms_output.put_line('Line 3 - a is not true'); ELSE dbms_output.put_line('Line 3 - a is true'); END IF; IF (NOT b) THEN dbms_output.put_line('Line 4 - b is not true'); ELSE dbms_output.put_line('Line 4 - b is true'); END IF; END; / When the above code is executed at SQL prompt, it produces the following result:
Line 2 - Condition is true
Line 3 - a is true Line 4 - b is not true PL/SQL procedure successfully completed. PL/SQL Operator Precedence Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator: For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Try the following example to understand the operator precedence available in PL/SQL:
DECLARE
a number(2) := 20; b number(2) := 10; c number(2) := 15; d number(2) := 5; e number(2) ; BEGIN e := (a + b) * c / d; -- ( 30 * 15 ) / 5 dbms_output.put_line('Value of (a + b) * c / d is : '|| e ); e := ((a + b) * c) / d; -- (30 * 15 ) / 5 dbms_output.put_line('Value of ((a + b) * c) / d is : ' || e ); e := (a + b) * (c / d); -- (30) * (15/5) dbms_output.put_line('Value of (a + b) * (c / d) is : '|| e ); e := a + (b * c) / d; -- 20 + (150/5) dbms_output.put_line('Value of a + (b * c) / d is : ' || e ); END; / When the above code is executed at SQL prompt, it produces the following result:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50 PL/SQL procedure successfully completed. |