Stored Procedures
Stored Procedures
PL/SQL Procedure:
A PL/SQL procedure is a reusable unit that encapsulates specific business logic of the
application.
Basic Syntax:
CREATE [OR REPLACE ] PROCEDURE procedure_name (parameter_list) IS
Declaration Statements;
BEGIN
[execution statements]
EXCEPTION
[exception handler]
END [procedure_name ];
• Procedures may or may not return a value.
• Procedures return more than one value while using the out
parameter.
• Procedure can execute only 3 ways
a) Anonymous Block
b) Exec
c) Call
Procedure can not execute in select statement.
Procedure internally having one time compilation process.
Procedure are used to improve the performance of business
applications
Every procedure is having two parts
a) Procedure Specification
In procedure specification, we are specifying the name of the
procedure and types of the parameters.
b) Procedure Body
In the procedure body we are solving the actual task
PL/SQL procedure header
• A procedure begins with a header that specifies its name and an optional
parameter list.
• Each parameter can be in either IN, OUT, or INOUT mode.
• The parameter mode specifies whether a parameter can be read from or write to.
IN
An IN parameter is read-only. An IN parameter is referred inside a procedure, but
value cannot be changed. IN as the default mode.
OUT
An OUT parameter is writable. A return value is set for the OUT parameter and
return it to the calling program.
INOUT
An INOUT parameter is both readable and writable. The procedure can read and
modify it.
Procedure parts
PL/SQL procedure body
The procedure body has three parts. The executable part is mandatory whereas
the declarative and exception-handling parts are optional. The executable part
must contain at least one executable statement.
1) Declarative part
In this part, you can declare variables, constants, cursors, etc. a declaration part
of a procedure does not start with the DECLARE keyword.
2) Executable part
This part contains one or more statements that implement specific business
logic. It might contain only a NULL statement.
3) Exception-handling part
This part contains the code that handles exceptions.
Example
CREATE OR REPLACE PROCEDURE print_contact (in_customer_id NUMBER)
IS
r_contact contacts%ROWTYPE;
BEGIN
SELECT * INTO r_contact FROM contacts
WHERE customer_id = p_customer_id;
dbms_output.put_line( r_contact.first_name || ' ‘||r_contact.last_name
|| '<' || r_contact.email ||'>' );
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line( SQLERRM );
END;
Sub Programs
SUB PROGRAMS
• Sub programs are named pl/sql blocks which is used to solve
particular task.
• There are two types of sub programs supported by oracle.
1) Procedures
2) Function
Ex :
create or replace procedure p11(p_empno number) is
v_ename varchar2(10);
v_sal number(10);
begin
select ename,sal into v_ename,v_sal from emp
where empno=p_empno;
dbms_output.put_line(v_ename||','||v_sal);
end
Executing the Procedure
Execute The Procedure in 3 methods:
Method: 1
Exec P11 ( 7902 )
Method: 2 –
Begin
P11 ( 7902 );
end;
Method: 3
Call P11 ( 7902 )
Example:
create or replace procedure p111(p_deptno number) is
cursor c1 is select * from emp where deptno=p_deptno;
i emp%rowtype;
begin
open c1;
loop
fetch c1 into i;
exit when c1%notfound;
dbms_output.put_line(i.ename||','||i.sal||','||i.deptno);
end loop;
close c1;
end;
Parameters in Procedures
Parameters are used to pass the value into procedures and also return
values from the procedure.
In this case, we must use two types of parameters.
a. Formal Parameters
b. Actual Parameters
a) Formal Parameters
Formal Parameters are defined in the procedure specification
In Formal Parameters, we are defining the parameter name &
mode of the parameters
Three Modes
1) IN Mode
2) OUT Mode
3) IN OUT Mode
IN Mode
IN Mode:
• By default procedure parameters have IN mode.
• IN Mode is used to pass the values into the procedure body.
• This mode behaves like a constant in the procedure body, through this
IN Mode we can also pass
• Default values using default or ":=" operator
Create or replace procedure P1 ( p_deptno in number, p_dname in varchar2,
p_loc in varchar2) is
begin
insert into dept values (p_deptno, p_dname,p_loc);
dbms_output.put_line('record is inserted through procedure');
End;
Execution of procedures:
There are three types of execution methods supported by in parameter.
1) Positional Notations
2) Named Notations
3) Mixed Notation
1) Positional Notations
exec p1( 1, ‘R & D',‘Chennai');
2) Named Notations
exec p1 (p_dname=>‘Manufacturing', p_loc=>‘Pondy', p_deptno=>2)
3) Mixed Notations
exec p1 ( 3, p_dname=>‘Coimbatore', p_loc=>‘Finance' );
____________________________________________________________
2) OUT Mode :
• This mode is used to return values from procedure body.
• OUT Mode internally behaves like a uninitialized variable in procedure
body
Example
Create or replace procedure p1 (a in number, b out number) is
begin
b:=a*a;
dbms_output.put_line(b);
end;
If subprograms contain OUT, IN OUT Parameters those subprograms are
executed using the following two methods.
Method - 1 : Using Bind Variable
Method - 2 : Using Annonymous Block
Bind Variable:
These variables are session variables.
These variables are created at host environment that's why these
variables are also called as host variables.
These variables are not a pl/sql variables, but we can also use
these variables in PL/SQL to execute subprograms having OUR
Parameters
Method - 1: Bind Variable
Variable b number;
exec p1 (10, :b);
Method – 2:
Anonymous Block:
Declare b number(10);
begin
p1( 5, b );
dbms_output.put_line( b );
end;
Passing employee name as in parameter return salary
of that employee using out parameter from emp table
Create or replace procedure p1 ( p_ename in varchar2, p_sal out number ) is
begin
select sal into p_sal from emp where emp_name=p_ename;
end;
Method - 1 :
Bind variable
variable a number;
exec p1 ( 'KING', :a);
Method - 2 :
Anonymous Block:
Declare
a number(10);
begin
exec p1( ' ALLEN ', a );
dbms_output.put_line( a );
end;
Passing deptno as a parameter returns how many employees are
working in a dept from emp table