Wednesday, January 9, 2013

Declaring Variables


What is a variable?

In Oracle/PLSQL, a variable allows a programmer to store data temporarily during the execution of code.

Syntax

The syntax for declaring variables is:
variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]

Declaring a variable

Below is an example of how to declare a variable called LDescription.
LDescription varchar2(40);
You can then later set or change the value of the LDescription variable, as follows:
LDescription := 'techonthenet.com Example';

Declaring a variable with an initial value (not a constant)

Below is an example of how to declare a variable and give it an initial value. This is different from a constant in that the variable's value can be changed later.
LType varchar2(10) := 'techonthenet.com Example';
You could later change the variable's value, as follows:
LDescription := 'My value has changed';

Declaring a constant

Below is an example of how to declare a constant. The value of a constant can not be changed.
LTotal CONSTANT numeric(8,1) := 8363934.1;