procedure and functions. procedures syntax: [create [or replace]] procedure pname [(p1, p2…)][is |...

37
Procedure and Functions

Upload: erik-crowder

Post on 14-Jan-2016

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Procedure and Functions

Page 2: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Procedures Syntax:[CREATE [OR REPLACE]] PROCEDURE

Pname[(p1, p2…)] [IS | AS]

[declarations]BEGIN

[Execution Statements][EXCEPTIONS

exception_handling]END [Pname];/

2

Page 3: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

ExampleCREATE PROCEDURE PrintNameIS

Address VARCHAR2(50);BEGIN

Address := ‘102 Main St, Frostburg MD 21532’;

DBMS_OUTPUT.PUT_LINE(Address);END PrintName;/

3

Page 4: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Run the ProcedureEXEC PrintName();EXEC PrintName;

4

Page 5: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Procedure with ParameterCREATE OR REPLACE PROCEDURE Print_B_Date (V_ID

NUMBER)

IS

V_B_Date DATE;

BEGINSELECT B_Date INTO V_B_Date

FROM Employee

WHERE ID=V_ID;

DBMS_OUTPUT.PUT_LINE(V_B_Date);

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE(‘No Data Found’);

END Print_B_Date;

/

5

Page 6: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Call Procedure & Check for ErrorsEXEC Print_B_Date(112);

SHOW ERRORS;

6

Page 7: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

ExampleCREATE PROCEDURE Largest (P1 NUMBER, P2 NUMBER)ISBEGIN

DBMS_OUTPUT.PUT_LINE(‘The First Number is’ || P1);DBMS_OUTPUT.PUT_LINE(‘The Second Number is’ || P2);

IF(P1>P2) THENDBMS_OUTPUT.PUT_LINE(‘The Largest Number is’ || P1);

ELSE DBMS_OUTPUT.PUT_LINE(‘The Largest Number is’ || P2);

END IF;END Largest;/

7

Page 8: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Call ProcedureEXEC Largest(12, 24);EXEC Largest(24, 12);

8

Page 9: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Procedure with default valueCREATE PROCEDURE Data ( Name

VARCHAR2, B_Date DATE DEFAULT SYSDATE)

ISBEGIN

DBMS_OUTPUT.PUT_LINE(Name || B_Date);

END;/

9

Page 10: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Procedure CallEXEC Data(‘Jim Smith’, ‘12-MAR-03’)EXEC Data(‘Mary Show’);

10

Page 11: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Dropping ProcedureDROP PROCEDURE Largest;

11

Page 12: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Functions Syntax:[CREATE [OR REPLACE]] FUNCTION Fname[(p1, p2…)] RETURN datatype [IS | AS]

[declarations]BEGIN

[Execution Statements][EXCEPTIONS

exception_handling]END [Fname];/

12

Page 13: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

ExampleCREATE FUNCTION PrintNameF RETURN

BOOLEANIS

Address VARCHAR2(50);BEGIN

Address := ‘102 Main St, Frostburg MD 21532’;

DBMS_OUTPUT.PUT_LINE(Address);RETURN true;

END PrintNameF;/

13

Page 14: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Run a Function:DECLARE

flag BOOLEAN;

BEGIN Flag:=PrintNameF;IF flag THEN

DBMS_OUTPUT.PUT_LINE(‘Function Printed Correctly’));

END IF;

END;/

14

Page 15: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Function with ParameterCREATE OR REPLACE FUNCTION Print_B_DateF (V_ID NUMBER) RETURN

CHAR IS

V_B_Date DATE;found CHAR(1):=‘F’;F_ID NUMBER:=V_ID;

BEGINSELECT B_Date INTO V_B_Date FROM EmployeeWHERE ID=F_ID;DBMS_OUTPUT.PUT_LINE(V_B_Date);found:=‘T’;RETURN found;

EXCEPTIONWHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE(‘No Data Found’);END Print_B_DateF;/

15

Page 16: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Calling a Function:

SELECT Print_B_DateF(102) FROM DUAL;

16

Page 17: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Dropping Function

DROP FUNCTION Print_B_DateF;

17

Page 18: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Find the largest ValueDECLARE –--p1, p2, p3, p4

large NUMBER;BEGIN

large:=p1;IF large< p2 THEN large:=p2; END IF;IF large< p3 THEN large:=p3; END IF;IF large< p4 THEN large:=p4; END IF;

END;/

18

Page 19: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

PACKAGESDBMS_OUTPUT.NEW_LINE;

DBMS_OUTPUT.PUT_LINE ('Display This’);

19

Page 20: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

PACKAGE HEADER (Specification)CREATE [ OR REPLACE] PACKAGE p_name IS

|AS-- functions-- procedures-- variables-- constants -- exceptions-- cursors

END [p_name ];

20

Page 21: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

EXAMPLECREATE PACKAGE myPackage AS

PROCEDURE PrintLarge (P1 NUMBER, p2 NUMBER);

FUNCTION FindLarge (P1 NUMBER, P2 NUMBER) RETURN NUMBER;

END myPackage;

21

Page 22: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

PACKAGE BODYCREATE OR REPLACE PACKAGE BODY

p_name IS|AS--package body

End p_name;

22

Page 23: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

EXAMPLECREATE OR REPLACE PACKAGE BODY MyPackage AS

PROCEDURE PrintLarge (P1 NUMBER, p2 NUMBER) ASBEGIN IF p1> p2 THEN

DBMS_OUTPUT.PUT_LINE(p1); ELSE

DBMS_OUTPUT.PUT_LINE(p2); END IF;

END PrintLarge;FUNCTION FindLarge (P1 NUMBER, P2 NUMBER) RETURN NUMBER AS

large NUMBER;BEGIN IF p1> p2 THEN

LARGE:=p1; ELSE

LARGE:=p2; END IF;

RETURN LARGE;END ;

END myPackage;23

Page 24: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Call Elements of Package:EXEC MyPackage.PrintLarge (32 , 11);

SELECT MyPackage.FindLarge( 22 , 43)

FROM DUAL;

24

Page 25: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

EXAMPLE 2CREATE OR REPLACE PACKAGE Employee_p

ASFUNCTION E_Full_Name (Last_N testdata.employee.L_name%TYPE,

First_N testdata.employee.F_name%TYPE) RETURN VARCHAR2;

FUNCTION E_Name(E_id testdata.employee.id%TYPE) RETURN VARCHAR2;

END Employee_p;

25

Page 26: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

EXAMPLE 2CREATE OR REPLACE PACKAGE BODY

Employee_P ASFUNCTION E_Full_Name

(Last_N Employee.L_name%TYPE, First_N Employee.F_name%TYPE)RETURN VARCHAR2 IS

BEGINRETURN Last_N || ‘, ‘ || First_N;

END;

26

Page 27: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

EXAMPLE 2FUNCTION E_Name(f_id EMPLOYEE.id%TYPE)

RETURN VARCHAR2 IStemp VARCHAR2(200);id EMPLOYEE.id%TYPE:=f_id;

BEGINSELECT (L_name || ‘ ‘ ||F_Name) INTO tempFROM employee WHERE employee.id = id;RETURN temp;

EXCEPTIONWHEN NO_DATA_FOUND THEN

RETURN NULL;WHEN TOO_MANY_ROWS THEN

…..END;

END Employee_P;27

Page 28: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

CALL FUNCTIONS of PACKAGESELECT Employee_p.E_NAME(107)FROM DUAL;

28

Page 29: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

29

Subprogram LocationUSER_OBJECTSUSER_SOURCEUSER_ERRORS

SELECT OBJECT_NAME, OBJECT_TYPE, STATUSFROM USER_OBJECTSWHERE OBJECT_NAME = ‘PrintInfo’;

OBJECT_NAME OBJECT_TYPE STATUS

SIMPLE PROCEDURE VALID

Page 30: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

30

Subprogram LocationSELECT TEXTFROM USER_SOURCEWHERE NAME = ‘PrintInfo’ AND TYPE= ‘PROCEDURE’

ORDER BY line;

TEXT-----------------------------------------

CREATE OR REPLACE PROCEDURE PrintInfo ASss NUMBER:=24;

BEGINDBMS_OUTPUT.PUT_LINE(ss);

END PrintInfo;

Page 31: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

31

Find Errors:SHOW ERRORS;

-Line number of the error-Column number of the error-Text message of the error

SELECT line, position, textFROM USER_ERRORSWHERE name=‘PrintInfo’ AND

TYPE=‘PROCEDURE’;

Page 32: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Exception HandlingCREATE FUNCTION Employee_P (f_id EMPLOYEE.id%TYPE)

RETURN VARCHAR2 IStemp VARCHAR2(200);id EMPLOYEE.id%TYPE:=f_id;

BEGINSELECT (L_name || ‘ ‘ ||F_Name) INTO tempFROM employee WHERE employee.id = id;RETURN temp;

EXCEPTIONWHEN NO_DATA_FOUND THENRETURN NULL;WHEN TOO_MANY_ROWS THEN

INSERT INTO errorsVALUES (‘Error in statement ‘); RETURN NULL;

END Employee_P;32

Page 33: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

33

DECLARE

BEGINEXCEPTIONWHEN ZERO_DIVIDE THEN

INSERT INTO table1(info)VALUES (info_data);COMMIT;

WHEN OTHERS THENROLLBACK;

END;

Exception Handling

Page 34: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

34

Exception Types:1. Predefined Oracle Server errors:

No declaration is neededORA-01403 NO_DATA_FOUNDORA-01422 TOO_MANY_ROWSORA-01476 ZERO_DIVIDEORA-06500 STORAGE_ERRORORA-06530 ACCESS_INTO_NULLORA-06592 CASE_NOT_FOUNDORA-00001 DUP_VAL_ON_INDEX

Page 35: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

35

Exception Types:2. User_defined

User determines an abnormal condition Declared in declaration section

DECLAREe_TooManyNumbers EXCEPTION;

Page 36: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

36

Raising USER_DEFINED ExceptionsExample

DECLAREe_TooManyNumbers EXCEPTION;V_NoStudent NUMBER(4);V_MaxStudent NUMBER(4);

BEGINSELECT Current_Students, Max_StudentsINTO V_NoStudent, V_MaxStudentFROM classesWHERE C_Num=455 AND Dept=‘ITEC’;IF V_NoStudent > V_MaxStudent THEN

RAISE e_TooManyNumbers;END IF;

Page 37: Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS

Chapter 18: Exception Handling 37

EXCEPTIONWHEN e_TooManyNumbers THEN

INSERT INTO log_file (info)VALUES (‘ITEC455 has:‘ || V_NoStudent || ‘Max No

is:’ || V_MaxStudent);

END;