Assigning Values to a Variable With the Assignment Operator

Previous
Previous
Next
Next

You can assign values to a variable in several ways. One way uses the assignment operator (:=), a colon followed by an equal sign, as shown in Example: Assigning Values to Variables With the PL/SQL Assignment Operator. You place the variable to the left of the operator and an expression, including function calls, to the right. Note that you can assign a value to a variable when it is declared.

Assigning Values to Variables With the PL/SQL Assignment Operator

DECLARE  -- declare and assiging variables
   wages          NUMBER(6,2);
   hours_worked   NUMBER := 40;
   hourly_salary  NUMBER := 22.50;
   bonus          NUMBER := 150;
   country        VARCHAR2(128);
   counter        NUMBER := 0;
   done           BOOLEAN := FALSE;
   valid_id       BOOLEAN;
BEGIN
   wages := (hours_worked * hourly_salary) + bonus;  -- compute wages
   country := 'France'; -- assign a string literal
   country := UPPER('Canada'); -- assign an uppercase string literal
   done := (counter > 100); -- assign a BOOLEAN, in this case FALSE
   valid_id := TRUE; -- assign a BOOLEAN
END;
/