top of page
Writer's picturecompnomics

Assignments and Expressions in PL/SQL


Assignments

In PL/SQL, assignments are used to assign values to variables. The assignment operator is :=.

Syntax:QL

variable_name := expression;

Example:

DECLARE
  v_salary NUMBER(10,2) := 50000;
  v_name VARCHAR2(50) := 'John Doe';
BEGIN
  -- ...
END;
/

Expressions

Expressions are combinations of operators, operands, and function calls that evaluate to a value.


Types of Expressions:

  1. Arithmetic Expressions:

    • Use arithmetic operators like +, -, *, /, and %.

    • Example: x + y * 2

  2. Relational Expressions:

    • Use comparison operators like =, !=, <, >, <=, and >=.

    • Example: age > 18

  3. Logical Expressions:

    • Use logical operators like AND, OR, and NOT.

    • Example: (age > 18) AND (salary > 50000)

  4. String Expressions:

    • Use string concatenation operator ||.

    • Example: 'Hello, ' || name


Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. The following table shows the precedence of operators in PL/SQL, from highest to lowest:

Operator

Description

NOT

Logical NOT

*, /, %

Multiplication, division, modulo

+, -

Addition, subtraction

`

`

=, !=, <, >, <=, >=`

Comparison operators

AND

Logical AND

OR

Logical OR

Example:

DECLARE
  x NUMBER := 10;
  y NUMBER := 5;
  z NUMBER;
BEGIN
  z := x + y * 2; -- z will be 20
  z := (x + y) * 2; -- z will be 30
END;
/

By understanding assignments, expressions, and operator precedence, you can effectively write complex PL/SQL code to manipulate data and perform calculations.

5 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page