Download presentation
1
Stored Procedures Functions Packages
UNIT V Stored Procedures Functions Packages
2
PL/SQL Module types Anonymous block Procedures Functions
A module is a logical unit of work that performs a specific task. Anonymous block Procedures Functions
3
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.
4
Advantages of subprograms
Modularity: manageable, well defined logical modules Reusability: can be used in any number of applications Maintainability : easy to maintain smaller modules
5
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.
6
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.
7
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; /
8
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;
9
Procedure call declare radius number(1); begin area_circle(&radius); dbms_output.put_line('Back to main program'); end; / Enter value for radius: 5 The area of circle with radius 5 is 78.5 Back to main program
10
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.
11
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; /
12
Procedure call declare radius number(1); area number; begin area_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: 9 In the procedure: The area of circle with radius 9 is Back to main program. Area of circle with radius 9 is
13
Procedure –IN OUT Mode create 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; /
14
Procedure call –in out mode
declare 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; /
15
Result Enter value for b: 9 In main program: Radius is 9
In procedure radius of circle is 9 In procedure area of circle is Back to main program Area of circle is PL/SQL procedure successfully completed.
16
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.
17
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: 100 Enter value for name: ‘SMITH’ PL/SQL procedure successfully completed.
18
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.
19
Syntax to create a function
CREATE [OR REPLACE] FUNCTION function_name (parameters) RETURN return_datatype IS Declaration_section BEGIN Execution_section Return return_variable; EXCEPTION exception section END; Return Type: The return datatype can be any of the oracle datatypes like varchar, number etc. The execution and exception section both should return a value which is of the datatype defined in the header section.
20
Function example create or replace function area_circle_func (r number ) return number is a number; pi constant number(3,2):=3.14; begin a:=pi*r*r; dbms_output.put_line('In the function: The area of circle with radius '||r ||' is '||a); return a; end; /
21
Function call declare radius number(1); a number; begin a:=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: 6 In the function:The area of circle with radius 6 is Area of circle with radius is Back to main program
22
Differences Procedures 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.
23
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.
24
Components of a package
HAS TWO COMPONENTS Package specification: contains Name of the package Declaration of datatypes, variables, exceptions and subprograms. Package body : contains implementation part.
25
Package specification
CREATE 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;
26
Package body CREATE OR REPLACE PACKAGE BODY empl_mngmt IS PROCEDURE hire(empid number,name varchar2,salary number) AS BEGIN Insert 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); Select 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;
27
Calling a procedure of a package
Begin Empl_mngmt.hire(1111,’AKSHAT’,6500); End; PL/SQL Procedure successfully completed.
28
thanks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.