In PL/SQL,
operators are used to construct expressions by combining operands (variables, constants, or literals). PL/SQL evaluates expressions based on the operators' rules and precedence. This lesson covers key operators and demonstrates their use within PL/SQL blocks for practical application.
- Logical Operators:
Logical operators
AND
, OR
, and NOT
evaluate conditions based on a truth table, returning TRUE
, FALSE
, or NULL
. AND
and OR
are binary operators, while NOT
is unary. These are commonly used in conditional statements like IF
.
Examples:
DECLARE
v_age NUMBER := 25;
v_salary NUMBER := 50000;
v_department VARCHAR2(20) := 'Sales';
BEGIN
-- Using NOT
IF NOT (v_age < 18) THEN
DBMS_OUTPUT.PUT_LINE('You are an adult.');
ELSE
DBMS_OUTPUT.PUT_LINE('You are a minor.');
END IF;
-- Using AND
IF v_salary > 40000 AND v_age >= 25 THEN
DBMS_OUTPUT.PUT_LINE('Eligible for senior role.');
ELSE
DBMS_OUTPUT.PUT_LINE('Not eligible for senior role.');
END IF; -- Using OR
IF v_department = 'Sales' OR v_department = 'Marketing' THEN
DBMS_OUTPUT.PUT_LINE('You are in a client-facing department.');
ELSE
DBMS_OUTPUT.PUT_LINE('You are not in a client-facing department.');
END IF; -- Combining NOT, AND, OR
IF NOT (v_department = 'HR') AND (v_salary >= 50000 OR v_age < 30) THEN
DBMS_OUTPUT.PUT_LINE('Special compensation review needed.');
ELSE
DBMS_OUTPUT.PUT_LINE('Standard compensation review.');
END IF;
END;
- Comparison Operators: Comparison operators (
=
, !=
, <
, >
, <=
, >=
) compare expressions, returning TRUE
, FALSE
, or NULL
. They are used in conditional control statements and SQL queries within PL/SQL.