0% found this document useful (0 votes)
21 views31 pages

Stored Procedures

Uploaded by

s g shrinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views31 pages

Stored Procedures

Uploaded by

s g shrinivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Procedure

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

create or replace procedure pe2 ( p_deptno in number, p_t out number) is


begin
select count(*) into p_t from emp where deptno=p_deptno;
dbms_output.put_line(p_t);
end;
IN OUT MODE
IN OUT Mode
This mode is used to pass the values into sub program & return the
values from sub programs.

Create or replace procedure p1 ( a in out number ) is


begin
a := a*a;
dbms_output.put_line ( a );
end;
Execution Methods
Method - 1 :
Bind Variable:
variable a number;
exec :a :=10;
exec p1 (:a);
Method - 2:
Anonymous Block:
Declare a number(10) := &n;
begin p1(a);
dbms_output.put_line (a);
end;
Example
Create or replace procedure pe4 (a in out number) is
begin
select sal into a from emp where empno=a;
dbms_output.put_line(a);
end;
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;
EXEC print_contact(100);
Example
CREATE OR REPLACE PROCEDURE adjust_salary(in_employee_id IN
EMPLOYEES.EMPLOYEE_ID%TYPE, in_percent IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary + salary * in_percent / 100
WHERE employee_id = in_employee_id;
END;
SELECT salary FROM employees WHERE employee_id = 200;
exec adjust_salary(200,5);
SELECT salary FROM employees WHERE employee_id = 200;
Points to Ponder
Creating a Stored Procedure or Function
• Whenever a block of code for stored procedure or function is written
it is then, they are automatically compiled by the oracle engine.
• During compilation if any error occurs, we get a message on the
screen saying that the procedure or function is created with
compilation errors but actual error is not displayed.
• In order to find out the compilation errors following statement can be
executed:
SELECT * from USER_ERRORS;
Once it is compiled, it is then stored by the oracle engine in the
database as a database object.
Contd.,
• Stored Procedure or function's block of code in PL/SQL is made up of
the following three sections:
• Declarative section: In this section, variables, constants, cursor or
exceptions that are going to be used by procedure or function are
declared.
• Executable section: In this section, the definition of procedure or
function created is written. This section also contains the SQL or
PL/SQL statements assigning values, controlling execution and
manipulating data.
• Exception Handling section: In this section, the expected exceptions
are written which may arise during execution of code written in
executable part. This section is optional.
Advantages of a Stored Procedure or
Function
• Improves Database Performance
• Provides Reusability and avoids redundancy
• Maintains Integrity
• Maintains Security
• Saves Memory
Procedures Vs Functions

Return datatype will not be specified at the time of


Return datatype is mandatory at the time of creation
creation
Similarities between Procedure
and Function
• Both can be called from other PL/SQL blocks.
• If the exception raised in the subprogram is not handled in the
subprogram exception handling section, then it will propagate to the
calling block.
• Both can have as many parameters as required.
• Both are treated as database objects in PL/SQL.

You might also like