pl/sql subprograms

25
PL/SQL PL/SQL Subprograms Subprograms Session - IV Session - IV

Upload: klaus

Post on 20-Jan-2016

67 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: PL/SQL Subprograms

PL/SQL PL/SQL SubprogramsSubprograms

PL/SQL PL/SQL SubprogramsSubprograms

Session - IVSession - IVSession - IVSession - IV

Page 2: PL/SQL Subprograms

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

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

E.g.:-E.g.:-

Declare

PROCEDURE TEST IS

DBMS_OUTPUT.PUT_LINE(‘Testing’);

End;

Begin

Test;

End;

Page 5: PL/SQL Subprograms

ProcedureProcedure

Is Subprogram that performs a specific action.

Has two parts the specification and body.

Page 6: PL/SQL Subprograms

Specification begins with the keyword PROCEDURE and end with Procedure Name.

Parameter Declaration are

optional.

Page 7: PL/SQL Subprograms

Procedure body begins with the keyword END followed by an optional Procedure Name.

Page 8: PL/SQL Subprograms

The Procedure body has 3 Parts

Declarative PartExecutable partOptional Exception-Part

Page 9: PL/SQL Subprograms

Declarative Part Contains Local Declarations which are placed between IS and BEGIN.

Page 10: PL/SQL Subprograms

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

Parameter ModesParameter ModesParameter ModesParameter Modes

INOUT IN OUT

Page 12: PL/SQL Subprograms

ININININ

Lets you pass value to the subprogram being called.

Page 13: PL/SQL Subprograms

OUTOUTOUTOUT

Lets you pass return values to the caller of a Subprogram

Page 14: PL/SQL Subprograms

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

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

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

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

DECLAREA number(30);BEGINA:=F1(&N);DBMS_OUTPUT.PUT_LINE(A);END;

( OR )

SELECT F1(7369) from dual;

Page 19: PL/SQL Subprograms

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

Program Objects are :-Program Objects are :-Program Objects are :-Program Objects are :-ProceduresProceduresFunctionsFunctionsVariablesVariablesConstantsConstantsCursorsCursorsExceptionsExceptions

ProceduresProceduresFunctionsFunctionsVariablesVariablesConstantsConstantsCursorsCursorsExceptionsExceptions

Page 21: PL/SQL Subprograms

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

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

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

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

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;