To the point guide to PL/SQL
Chapters
Working with PL/SQL Operators
PL/SQL Operators are special symbols which are used to perform particular operation on operands.
Operators Types:
Arithmetic operators
+ : The operator used for addition. 2+3 = 5.
- : The operator used for subtraction. 3 -2 = 1.
* : The operator used for Multiplication.
/ : The operator used for division. 10/5=2.
** : The exponentiation operator. 10**5 = 100,000
SQL> BEGIN DBMS_OUTPUT.PUT_LINE(2 + 2); --addition DBMS_OUTPUT.PUT_LINE(11 - 8); --subtraction DBMS_OUTPUT.PUT_LINE(4 * 4); --multiplication DBMS_OUTPUT.PUT_LINE(10 / 5); --division END; / 4 3 16 2 PL/SQL procedure successfully completed. SQL>
Logical operators
and: If both the operands are true then returns true or else returns false.
or: If any one operand is true then returns true or else returns false.
not: If a condition is true then returns false
The AND works as: | The OR works as: | The NOT works as: |
True && True = True True && False = False False && True = False False && False = False |
True || True = True True || False = True False || True = True False || False = False |
!True = False !False = True |
Example:
DECLARE x boolean := true; y boolean := true; z Boolean := false; BEGIN IF (x AND y) THEN dbms_output.put_line('AND case condition met'); END IF; IF (x OR z) THEN dbms_output.put_line('OR case condition met'); END IF; END; /
Result:
AND case condition met
OR case condition met
Comparison operators
These operators are used to compare one expression with another and returns Boolean result.
= : The equality operator used to verifies two operands are equal or not if equals returns true.
<>,! =, ~=: The inequality operator used to verify two operands are equal or not if not equals returns true.
< : The less than operator verifies if the value of left operand is less than the value of right operand if equals returns true.
> : The greater than operator verifies if the value of left operand is greater than the value of right operand if equals returns true.
<=: The less than or equal to operator verifies if the value of left operand is less than or equal to the value of right operand.
>=: The greater than or equal to operator verifies if the value of left operand is greater than or equal to the value of right operand.
Special Comparison Operators:
LIKE: is used for pattern match.
BETWEEN: is used for range of values deals with two values only.
IN: verifies if a value is present within a specified list of values.
IS NULL: verifies if a value is null
Example:
DECLARE x number (2) := 1; y number (2) := 12; BEGIN IF (x < y) then dbms_output.put_line('x is less than b!!!'); ELSE dbms_output.put_line('x is not less than b!!!'); END IF; IF (y between 11 and 15) THEN dbms_output.put_line('True'); ELSE dbms_output.put_line('False'); END IF; BEGIN compare('suman', 's'); END; /
Result:
x is less than b!!!
True
This was our introduction to PL/SQL operators, in next part we will learn using conditional statements.
Description
This tutorial focus on PL/SQL and covers the below topics
- What is PL/SQL?
- Environment Setup
- Variables
- Data Types
- Constants
- Operators
- Conditions
- Loops
- Strings
- Arrays
- Procedures
- Functions
- Cursors
- Records
- Exceptions
- Packages
- Triggers
- Collections
- Transactions
- Date & Time
- Object Oriented
- DBMS Output
If you found any error with any of the docs please let us know.
Learning Objectives
Learn PL/SQL from a beginners perspective, this guide can also help you if you are trying to brush up your PLSQL skills
Author: Subject Coach
Added on: 20th Apr 2015
You must be logged in as Student to ask a Question.
None just yet!