Team Fly 

Page 206

Assigning values to variables is very important when programming in PL/SQL or any other programming language. You are able to assign values to variables in any section of your program code, and may assign values to variables in the declare section. By defining your variable in the declare section is done to initialize values in advance of their use in the program or to define values that will be used as constants in your program. To assign a value to a variable in the declaration section, you would use the following format:

Variable_name    variable_type    := value;

NOTE
The important item that you should notice here is that we use the := to assign a value. This is the standard that will be used in PL/SQL.

You may also define variable values in the execution and exception sections of your PL/SQL program. Within the program, you would use the following format to assign a value to a variable:

Variable_name       := value;

To assign values to variables you use, let's look at a small program that assigns values in each section of a program:

 -- declaration section of the program
declare
      l_counter      number := 0; -- initiate value to 0
      l_today       date   := sysdate; -- assign system date to variable
      l_name varchar2(50);    -- variable is defined but has no value
-- execution section of the program
begin
      l_counter := l_counter + 1; -- add one to current value of counter
      l_name := 'YOGI THE CAT'; -- set a value to name variable
-- Error handling section of the program
exception
      -- Generic error handling to handle any type of error
      when others then
-- print out an error message
raise_application_error (-20100, 'error#' || sqlcode || ' desc: ' sqlerrm)
end;

NOTE
Oracle has some special variables that may be used in a PL/SQL program. In the example, we used the sqlcode and sqlerrm variables. These variables respectively represent the Oracle error number and the Oracle error message. You may use these to capture Oracle errors in your program.

Team Fly 
0225