PL/SQL Subprograms
description
Transcript of PL/SQL Subprograms
![Page 1: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/1.jpg)
PL/SQL PL/SQL SubprogramsSubprograms
PL/SQL PL/SQL SubprogramsSubprograms
Session - IVSession - IVSession - IVSession - IV
![Page 2: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/2.jpg)
What are SubPrograms?.What are SubPrograms?.
Subprograms are named PL/SQL block that can take parameters and be invoked.
Two Types of Subprogram.
PROCEDURESFUNCTIONS
![Page 3: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/3.jpg)
Where are They Allowed?Where are They Allowed?
Subprograms can be defined using any ORACLE tool that supports PL/SQL
Must be declared at the end of a declarative section after all other program objects.
![Page 4: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/4.jpg)
E.g.:-E.g.:-
Declare
PROCEDURE TEST IS
DBMS_OUTPUT.PUT_LINE(‘Testing’);
End;
Begin
Test;
End;
![Page 5: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/5.jpg)
ProcedureProcedure
Is Subprogram that performs a specific action.
Has two parts the specification and body.
![Page 6: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/6.jpg)
Specification begins with the keyword PROCEDURE and end with Procedure Name.
Parameter Declaration are
optional.
![Page 7: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/7.jpg)
Procedure body begins with the keyword END followed by an optional Procedure Name.
![Page 8: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/8.jpg)
The Procedure body has 3 Parts
Declarative PartExecutable partOptional Exception-Part
![Page 9: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/9.jpg)
Declarative Part Contains Local Declarations which are placed between IS and BEGIN.
![Page 10: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/10.jpg)
Declare
Procedure Pn is
enam varchar(15);
BeginSelect ename INTO enam from emp where
empno=7369;
DBMS_OUTPUT.PUT_LINE(ENAM);
End;
![Page 11: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/11.jpg)
Parameter ModesParameter ModesParameter ModesParameter Modes
INOUT IN OUT
![Page 12: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/12.jpg)
ININININ
Lets you pass value to the subprogram being called.
![Page 13: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/13.jpg)
OUTOUTOUTOUT
Lets you pass return values to the caller of a Subprogram
![Page 14: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/14.jpg)
IN OUTIN OUTIN OUTIN OUT
Lets you pass initial values to the subprogram and return updated values to the caller.
![Page 15: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/15.jpg)
FunctionFunctionFunctionFunction
A Function is a Subprogram that computes a value. Functions and Procedures are structured alike, Except that functions have a RETURN clause.
![Page 16: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/16.jpg)
FunctionFunctionFunctionFunctionCREATE OR REPLACE FUNCTION FUN (ID
NUMBER)
RETURN CHAR IS
NAME EMP.ENAME%TYPE;
BEGIN
SELECT ENAME INTO NAME FROM EMP WHERE EMPNO=ID;
RETURN(NAME);
END;
CREATE OR REPLACE FUNCTION FUN (ID NUMBER)
RETURN CHAR IS
NAME EMP.ENAME%TYPE;
BEGIN
SELECT ENAME INTO NAME FROM EMP WHERE EMPNO=ID;
RETURN(NAME);
END;
![Page 17: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/17.jpg)
CREATE OR REPLACE FUNCTION F1(N NUMBER) RETURN NUMBERISSALARY EMP.SAL%TYPE;BEGINSELECT SAL INTO SALARY FROM EMP WHERE EMPNO=N;RETURN SALARY;END;
![Page 18: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/18.jpg)
DECLAREA number(30);BEGINA:=F1(&N);DBMS_OUTPUT.PUT_LINE(A);END;
( OR )
SELECT F1(7369) from dual;
![Page 19: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/19.jpg)
PACKAGESPACKAGESPACKAGESPACKAGES
Is an encapsulated collection of related program objects stored together in the database.
Is an encapsulated collection of related program objects stored together in the database.
![Page 20: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/20.jpg)
Program Objects are :-Program Objects are :-Program Objects are :-Program Objects are :-ProceduresProceduresFunctionsFunctionsVariablesVariablesConstantsConstantsCursorsCursorsExceptionsExceptions
ProceduresProceduresFunctionsFunctionsVariablesVariablesConstantsConstantsCursorsCursorsExceptionsExceptions
![Page 21: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/21.jpg)
CREATE PACKAGESCREATE PACKAGESCREATE PACKAGESCREATE PACKAGES
There are Two Distinct Steps to Create Package
1. PACKAGE SPECIFICATION
2. PACKAGE BODY
There are Two Distinct Steps to Create Package
1. PACKAGE SPECIFICATION
2. PACKAGE BODY
![Page 22: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/22.jpg)
PACKAGE SPECIFICATIONPACKAGE SPECIFICATIONPACKAGE SPECIFICATIONPACKAGE SPECIFICATION
CREATE PACKAGE Command
<> can declare Program Objects
i.e.. Procedures ,Functions etc..
CREATE PACKAGE Command
<> can declare Program Objects
i.e.. Procedures ,Functions etc..
![Page 23: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/23.jpg)
PACKAGE BODYPACKAGE BODYPACKAGE BODYPACKAGE BODY
CREATE PACKAGE BODY Command
can declare and define Program Objects
CREATE PACKAGE BODY Command
can declare and define Program Objects
![Page 24: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/24.jpg)
CREATE OR REPLACE PACKAGE P56 ISPROCEDURE P4(ENO NUMBER);END;
CREATE OR REPLACE PACKAGE P56 ISPROCEDURE P4(ENO NUMBER);END;
![Page 25: PL/SQL Subprograms](https://reader035.fdocuments.in/reader035/viewer/2022081420/56814f19550346895dbcaaa7/html5/thumbnails/25.jpg)
CREATE OR REPLACE PACKAGE BODYCREATE OR REPLACE PACKAGE BODY P56 ISP56 ISPROCEDURE P4(ENO NUMBER) ISPROCEDURE P4(ENO NUMBER) ISDESG EMP.JOB%TYPE; DESG EMP.JOB%TYPE; BEGINBEGINSELECT JOB INTO DESG FROM EMP SELECT JOB INTO DESG FROM EMP WHERE EMPNO=ENO;WHERE EMPNO=ENO;DBMS_OUTPUT.PUT_LINE(DESG);DBMS_OUTPUT.PUT_LINE(DESG);END P4;END P4;
END P56;END P56;
CREATE OR REPLACE PACKAGE BODYCREATE OR REPLACE PACKAGE BODY P56 ISP56 ISPROCEDURE P4(ENO NUMBER) ISPROCEDURE P4(ENO NUMBER) ISDESG EMP.JOB%TYPE; DESG EMP.JOB%TYPE; BEGINBEGINSELECT JOB INTO DESG FROM EMP SELECT JOB INTO DESG FROM EMP WHERE EMPNO=ENO;WHERE EMPNO=ENO;DBMS_OUTPUT.PUT_LINE(DESG);DBMS_OUTPUT.PUT_LINE(DESG);END P4;END P4;
END P56;END P56;