UNIT V Stored Procedures Functions Packages
UNIT VStored Procedures
Functions
Packages
PL/SQL MODULE TYPESA module is a logical unit of work that performs a
specific task.
Anonymous block
Procedures
Functions
SUBPROGRAMS A subprogram is a named block of
PL/SQL.
There are two types of subprograms in PL/SQL namely Procedures and Functions
Every subprogram will have a declarative part, an executable part or body, and an exception handling part, which is optional.
ADVANTAGES OF SUBPROGRAMS Modularity: manageable, well defined
logical modules
Reusability: can be used in any number of applications
Maintainability : easy to maintain smaller modules
WHAT IS A STORED PROCEDURE? A stored procedure is a named PL/SQL block
which performs one or more specific task. A procedure has a specification and a body. The specification 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.
SYNTAX TO CREATE A PROCEDURE
CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters]
IS Declaration section BEGIN Execution section EXCEPTION Exception section END;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.
EXAMPLE - PROCEDURE create or replace procedure area_circle
(r number) is pi constant number(3,2):=3.14; area number(8,2); begin area:=pi*r*r; dbms_output.put_line(' The area of circle
with radius '||r ||' is '||area);end; /
HOW TO EXECUTE A STORED PROCEDURE? There are two ways to execute a
procedure.1) From the SQL prompt.
EXECUTE [or EXEC] procedure_name;
2) Within another procedure – simply use the procedure name.
procedure_name;
PROCEDURE CALL declare radius number(1); begin area_circle(&radius); dbms_output.put_line('Back to main
program'); end;/Enter value for radius: 5The area of circle with radius 5 is 78.5Back to main program
PARAMETER MODES IN :The value must be specified when calling
the procedure. The value of the parameter can not be overwritten by the procedure or function.
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.
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.
A procedure may or may not return any value.
PROCEDURE EXAMPLE- IN MODE AND OUT MODE
create or replace procedure area_circle_in_out(r in number,a out number ) is
pi constant number(3,2):=3.14;begin a:=pi*r*r; dbms_output.put_line('In the procedure:
The area of circle with radius '||r ||' is '||a); end; /
PROCEDURE CALLdeclareradius number(1);area number;beginarea_circle_in_out(&radius,area);dbms_output.put_line(‘Back to main program.’);dbms_output.put_line(‘Area of circle with radius ‘||
radius ||’is ‘|| area);end;/Enter value for radius: 9In the procedure: The area of circle with radius 9 is
254.34Back to main program.Area of circle with radius 9 is 254.34
PROCEDURE –IN OUT MODEcreate or replace procedure proctest(a in out
number)is
pi constant number(3,2):=3.14;begin
dbms_output.put_line('In procedure radius of circle is '|| a);a:=pi*a*a;dbms_output.put_line('In procedure area of circle is '|| a);
end;/
PROCEDURE CALL –IN OUT MODEdeclare
b number;begin
b:=&b;dbms_output.put_line('In main program: Radius is '||b);proctest(b);dbms_output.put_line('Back to main program');dbms_output.put_line('Area of circle is '|| b);
end;/
RESULTEnter value for b: 9In main program: Radius is 9In procedure radius of circle is 9In procedure area of circle is 254.34Back to main programArea of circle is 254.34
PL/SQL procedure successfully completed.
PROCEDURE EXAMPLE create or replace procedure sal_hike(hike
number,name emp.ename%type,hiked_sal out emp.sal%type) is
salary emp.sal%type;Begin
select sal into salary from emp where ename=name;hiked_sal:=salary+hike;
end; /
Procedure created.
PROCEDURE CALL declare
hiked emp.sal%type; name emp.ename%type; hike number;
begin hike:=&hike; name:=&name; sal_hike(hike,name,hiked); update emp set sal=hiked where ename =name;
end; /Enter value for hike: 100Enter value for name: ‘SMITH’
PL/SQL procedure successfully completed.
PL/SQL FUNCTIONS A function is a named PL/SQL Block
A function must always return a value.
A procedure may or may not return a value.
SYNTAX TO CREATE A FUNCTIONCREATE [OR REPLACE] FUNCTION function_name (parameters) RETURN return_datatype IS Declaration_section BEGIN Execution_section Return return_variable; EXCEPTION exception section Return return_variable; END; 1) Return Type: The return datatype can be any of the
oracle datatypes like varchar, number etc.2) The execution and exception section both should return a
value which is of the datatype defined in the header section.
FUNCTION EXAMPLEcreate or replace function area_circle_func
(r number ) return number is a number;pi constant number(3,2):=3.14;begina:=pi*r*r;dbms_output.put_line('In the function: The
area of circle with radius '||r ||' is '||a);return a;end;/
FUNCTION CALLdeclareradius number(1);a number;begina:=area_circle_func(&radius);dbms_output.put_line('Area of circle with radius '||
radius ||'is '|| a ||' Back to main program');end;/Enter value for radius: 6In the function:The area of circle with radius 6 is 113.04Area of circle with radius is 113.04 Back to main
program
DIFFERENCESPROCEDURES AND FUNCTIONS Function must return a value back to the
calling program. Procedure may or may not return a value.
A function must return only one value to the calling PL/SQL block. By defining multiple OUT parameters in a procedure, multiple values can be passed to the caller.
PACKAGE An Oracle object which holds other
objects within it. A database object that encapsulates
related PL/SQL types, subprograms, cursors, exceptions, variables and cursors.
A single entity that groups logically related PL/SQL types, items and subprograms.
Unlike stored programs, the package itself cannot be called.
COMPONENTS OF A PACKAGEHAS TWO COMPONENTS Package specification: contains
Name of the packageDeclaration of datatypes,
variables, exceptions and subprograms.
Package body : contains implementation part.
PACKAGE SPECIFICATIONCREATE OR REPLACE PACKAGE
empl_mngmt IS PROCEDURE hire(empid number,name
varchar2,salary number); PROCEDURE inc_sal(empid
number,sal_inc number);END empl_mngmt;
PACKAGE BODYCREATE OR REPLACE PACKAGE BODY empl_mngmt IS
PROCEDURE hire(empid number,name varchar2,salary number) ASBEGINInsert into emp(empno,ename,sal) values(empid,name,salary);END hire;PROCEDURE inc_sal(empid number,sal_inc number) IS curr_sal number(7,2);BEGINSelect sal into curr_sal from emp where emp.empno=empid;Update emp set sal=(curr_sal+ sal_inc) where emp.empno=empid;END inc_sal;
END empl_mngmt;
CALLING A PROCEDURE OF A PACKAGE
BeginEmpl_mngmt.hire(1111,’AKSHAT’,6500);End;
PL/SQL Procedure successfully completed.
THANKS