Stored Procedures
Stored Procedures
A stored procedure or in simple a proc is a named PL/SQL block which performs one or more
specific task. This is similar to a procedure in other programming languages. A procedure has a
header and a body. The header consists of the name of the procedure and the parameters or
variables passed to the procedure. The body consists or declaration section, execution section
and exception section similar to a general PL/SQL Block. A procedure is similar to an
anonymous PL/SQL Block but it is named for repeated usage.
IS - marks the beginning of the body of the procedure and is similar to DECLARE in anonymous
PL/SQL Blocks. The code between IS and BEGIN forms the Declaration section.
The syntax within the brackets [ ] indicate they are optional. By using CREATE OR REPLACE
together the procedure is created if no other procedure with the same name exists or the
existing procedure is replaced with the current code.
The below example creates a procedure ‘employer_details’ which gives the details of the
employee.
procedure_name;
NOTE: In the examples given above, we are using backward slash ‘/’ at the end of the
program. This indicates the oracle engine that the PL/SQL program has ended and it can begin
processing the statements.
When you create a procedure or function, you may define parameters. There are three types of parameters
that can be declared:
1. IN - The parameter can be referenced by the procedure or function. The value of the parameter can
not be overwritten by the procedure or function.
2. OUT - The parameter can not be referenced by the procedure or function, but the value of the
parameter can be overwritten by the procedure or function.
3. IN OUT - The parameter can be referenced by the procedure or function and the value of the
parameter can be overwritten by the procedure or function.
The following is a simple example of a procedure:
cursor c1 is
select course_number
from courses_tbl
where course_name = name_in;
BEGIN
open c1;
fetch c1 into cnumber;
if c1%notfound then
cnumber := 9999;
end if;
commit;
close c1;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;
This procedure is called UpdateCourse. It has one parameter called name_in. The procedure will lookup the
course_number based on course name. If it does not find a match, it defaults the course number to 99999. It
then inserts a new record into the student_courses table.
1) IN type parameter: These types of parameters are used to send values to stored
procedures.
2) OUT type parameter: These types of parameters are used to get values from stored
procedures. This is similar to a return type in functions.
3) IN OUT parameter: These types of parameters are used to send values and get values from
stored procedures.
NOTE: If a parameter is not explicitly defined a parameter type, then by default it is an IN
type parameter.
1) IN parameter:
This is similar to passing parameters in programming languages. We can pass values to the
stored procedure through these parameters or variables. This type of parameter is a read only
parameter. We can assign the value of IN type parameter to a variable or use it in a query, but
we cannot change its value inside the procedure.
2) OUT Parameter:
The OUT parameters are used to send the OUTPUT from a procedure or a function. This is a
write-only parameter i.e, we cannot pass values to OUT paramters while executing the stored
procedure, but we can assign values to OUT parameter inside the stored procedure and the
calling program can recieve this output value.
3) IN OUT Parameter:
The IN OUT parameter allows us to pass values into a procedure and get output values from the
procedure. This parameter is used if the value of the IN parameter can be changed in the
calling program.
By using IN OUT parameter we can pass values into a parameter and return a value to the
calling program using the same parameter. But this is possible only if the value passed to the
procedure and output value have a same datatype. This parameter is used if the value of the
parameter will be changed in the procedure.
The below examples show how to create stored procedures using the above three types of
parameters.
Example1:
Let’s create a procedure which gets the name of the employee when the employee id is
passed.
We can call the procedure ‘emp_name’ in this way from a PL/SQL Block.
1> DECLARE
2> empName varchar(20);
3> CURSOR id_cur SELECT id FROM emp_ids;
4> BEGIN
5> FOR emp_rec in id_cur
6> LOOP
7> emp_name(emp_rec.id, empName);
8> dbms_output.putline('The employee ' || empName || ' has id ' ||
emp-rec.id);
9> END LOOP;
10> END;
11> /
Example 2:
The below PL/SQL block shows how to execute the above 'emp_salary_increase' procedure.
1> DECLARE
2> CURSOR updated_sal is
3> SELECT empID,salary
4> FROM emp_tbl;
5> pre_sal number;
6> BEGIN
7> FOR emp_rec IN updated_sal LOOP
8> pre_sal := emp_rec.salary;
9> emp_salary_increase(emp_rec.empID, emp_rec.salary);
10> dbms_output.put_line('The salary of ' || emp_rec.empID ||
11> ' increased from '|| pre_sal || ' to '||
emp_rec.salary);
12> END LOOP;
13> END;
14> /