pl/sql subprograms
DESCRIPTION
PL/SQL Subprograms. Session - IV. What are SubPrograms?. Subprograms are named PL/SQL block that can take parameters and be invoked. Two Types of Subprogram. PROCEDURES FUNCTIONS. Where are They Allowed?. Subprograms can be defined using any ORACLE tool that supports PL/SQL - PowerPoint PPT PresentationTRANSCRIPT
PL/SQL PL/SQL SubprogramsSubprograms
PL/SQL PL/SQL SubprogramsSubprograms
Session - IVSession - IVSession - IVSession - IV
What are SubPrograms?.What are SubPrograms?.
Subprograms are named PL/SQL block that can take parameters and be invoked.
Two Types of Subprogram.
PROCEDURESFUNCTIONS
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.
E.g.:-E.g.:-
Declare
PROCEDURE TEST IS
DBMS_OUTPUT.PUT_LINE(‘Testing’);
End;
Begin
Test;
End;
ProcedureProcedure
Is Subprogram that performs a specific action.
Has two parts the specification and body.
Specification begins with the keyword PROCEDURE and end with Procedure Name.
Parameter Declaration are
optional.
Procedure body begins with the keyword END followed by an optional Procedure Name.
The Procedure body has 3 Parts
Declarative PartExecutable partOptional Exception-Part
Declarative Part Contains Local Declarations which are placed between IS and BEGIN.
Declare
Procedure Pn is
enam varchar(15);
BeginSelect ename INTO enam from emp where
empno=7369;
DBMS_OUTPUT.PUT_LINE(ENAM);
End;
Parameter ModesParameter ModesParameter ModesParameter Modes
INOUT IN OUT
ININININ
Lets you pass value to the subprogram being called.
OUTOUTOUTOUT
Lets you pass return values to the caller of a Subprogram
IN OUTIN OUTIN OUTIN OUT
Lets you pass initial values to the subprogram and return updated values to the caller.
FunctionFunctionFunctionFunction
A Function is a Subprogram that computes a value. Functions and Procedures are structured alike, Except that functions have a RETURN clause.
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;
CREATE OR REPLACE FUNCTION F1(N NUMBER) RETURN NUMBERISSALARY EMP.SAL%TYPE;BEGINSELECT SAL INTO SALARY FROM EMP WHERE EMPNO=N;RETURN SALARY;END;
DECLAREA number(30);BEGINA:=F1(&N);DBMS_OUTPUT.PUT_LINE(A);END;
( OR )
SELECT F1(7369) from dual;
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.
Program Objects are :-Program Objects are :-Program Objects are :-Program Objects are :-ProceduresProceduresFunctionsFunctionsVariablesVariablesConstantsConstantsCursorsCursorsExceptionsExceptions
ProceduresProceduresFunctionsFunctionsVariablesVariablesConstantsConstantsCursorsCursorsExceptionsExceptions
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
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..
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
CREATE OR REPLACE PACKAGE P56 ISPROCEDURE P4(ENO NUMBER);END;
CREATE OR REPLACE PACKAGE P56 ISPROCEDURE P4(ENO NUMBER);END;
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;