Skip Headers
Oracle® Database PL/SQL User's Guide and Reference
10g Release 2 (10.2)

Part Number B14261-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Feedback

Go to previous page
Previous
Go to next page
Next
View PDF

Assignment Statement

An assignment statement sets the current value of a variable, field, parameter, or element. The statement consists of an assignment target followed by the assignment operator and an expression. When the statement is executed, the expression is evaluated and the resulting value is stored in the target. For more information, see "Assigning Values to Variables".

Syntax

assignment statement ::=

Description of assignment_statement.gif follows
Description of the illustration assignment_statement.gif

Keyword and Parameter Description

attribute_name

An attribute of an object type. The name must be unique within the object type (but can be reused in other object types). You cannot initialize an attribute in its declaration using the assignment operator or DEFAULT clause. Also, you cannot impose the NOT NULL constraint on an attribute.

collection_name

A nested table, index-by table, or varray previously declared within the current scope.

cursor_variable_name

A PL/SQL cursor variable previously declared within the current scope. Only the value of another cursor variable can be assigned to a cursor variable.

expression

A combination of variables, constants, literals, operators, and function calls. The simplest expression consists of a single variable. For the syntax of expression, see "Expression Definition". When the assignment statement is executed, the expression is evaluated and the resulting value is stored in the assignment target. The value and target must have compatible datatypes.

field_name

A field in a user-defined or %ROWTYPE record.

host_cursor_variable_name

A cursor variable declared in a PL/SQL host environment and passed to PL/SQL as a bind variable. The datatype of the host cursor variable is compatible with the return type of any PL/SQL cursor variable. Host variables must be prefixed with a colon.

host_variable_name

A variable declared in a PL/SQL host environment and passed to PL/SQL as a bind variable. Host variables must be prefixed with a colon.

index

A numeric expression that must return a value of type PLS_INTEGER, BINARY_INTEGER, or a value implicitly convertible to that datatype.

indicator_name

An indicator variable declared in a PL/SQL host environment and passed to PL/SQL. Indicator variables must be prefixed with a colon. An indicator variable indicates the value or condition of its associated host variable. For example, in the Oracle Precompiler environment, indicator variables let you detect nulls or truncated values in output host variables.

object_name

An instance of an object type previously declared within the current scope.

parameter_name

A formal OUT or IN OUT parameter of the subprogram in which the assignment statement appears.

record_name

A user-defined or %ROWTYPE record previously declared within the current scope.

variable_name

A PL/SQL variable previously declared within the current scope.

Usage Notes

By default, unless a variable is initialized in its declaration, it is initialized to NULL every time a block or subprogram is entered. Always assign a value to a variable before using that variable in an expression.

You cannot assign nulls to a variable defined as NOT NULL. If you try, PL/SQL raises the predefined exception VALUE_ERROR. Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. You can assign the result of a comparison or other test to a Boolean variable.

You can assign the value of an expression to a specific field in a record. You can assign values to all fields in a record at once. PL/SQL allows aggregate assignment between entire records if their declarations refer to the same cursor or table. Example 1-2, "Assigning Values to Variables With the Assignment Operator" shows how to copy values from all the fields of one record to another:

You can assign the value of an expression to a specific element in a collection, by subscripting the collection name.

Examples

Example 13-1 illustrates various ways to declare and then assign values to variables.

Example 13-1 Declaring and Assigning Values to Variables

DECLARE
  wages         NUMBER;
  hours_worked  NUMBER := 40; 
  hourly_salary CONSTANT NUMBER := 17.50; -- constant value does not change
  country       VARCHAR2(64) := 'UNKNOWN';
  unknown       BOOLEAN;
  TYPE comm_tab IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
  commissions   comm_tab;
  TYPE jobs_var IS VARRAY(10) OF employees.job_id%TYPE;
  jobids        jobs_var;
  CURSOR c1 IS SELECT department_id FROM departments; -- cursor declaration
  deptid        departments.department_id%TYPE;
  emp_rec       employees%ROWTYPE; -- do not need TYPE declaration in this case
BEGIN
/* the following are examples of assignment statements */
  wages := hours_worked * hourly_salary; -- compute wages
  country := UPPER('italy');
  unknown := (country = 'UNKNOWN');
  commissions(5) := 20000 * 0.15; commissions(8) := 20000 * 0.18;
  jobids := jobs_var('ST_CLERK'); jobids.EXTEND(1); jobids(2) := 'SH_CLERK';
  OPEN c1; FETCH c1 INTO deptid; CLOSE c1;
  emp_rec.department_id := deptid; emp_rec.job_id := jobids(2);
END;
/

For examples, see the following:


Example 1-2, "Assigning Values to Variables With the Assignment Operator"
Example 1-3, "Assigning Values to Variables by SELECTing INTO"
Example 1-4, "Assigning Values to Variables as Parameters of a Subprogram"
Example 2-10, "Assigning Values to a Record With a %ROWTYPE Declaration"

Related Topics


"Assigning Values to Variables"
"Constant and Variable Declaration"
"Expression Definition"
"SELECT INTO Statement"