Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS."— Presentation transcript:

1 Procedure and Functions

2 Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS exception_handling] END [Pname]; / 2

3 Example CREATEPROCEDUREPrintName IS AddressVARCHAR2(50); BEGIN Address := ‘102 Main St, Frostburg MD 21532’; DBMS_OUTPUT.PUT_LINE(Address); ENDPrintName; / 3

4 Run the Procedure EXECPrintName(); EXECPrintName; 4

5 Procedure with Parameter CREATE OR REPLACE PROCEDURE Print_B_Date (V_ID NUMBER) IS V_B_DateDATE; BEGIN SELECT B_Date INTOV_B_Date FROMEmployee WHEREID=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

6 Call Procedure & Check for Errors EXEC Print_B_Date(112); SHOW ERRORS; 6

7 Example CREATE PROCEDURE Largest (P1 NUMBER, P2 NUMBER) IS BEGIN DBMS_OUTPUT.PUT_LINE(‘The First Number is’ || P1); DBMS_OUTPUT.PUT_LINE(‘The Second Number is’ || P2); IF(P1>P2) THEN DBMS_OUTPUT.PUT_LINE(‘The Largest Number is’ || P1); ELSE DBMS_OUTPUT.PUT_LINE(‘The Largest Number is’ || P2); END IF; END Largest; / 7

8 Call Procedure EXEC Largest(12, 24); EXEC Largest(24, 12); 8

9 Procedure with default value CREATE PROCEDUREData ( Name VARCHAR2, B_Date DATE DEFAULT SYSDATE) IS BEGIN DBMS_OUTPUT.PUT_LINE(Name || B_Date); END; / 9

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

11 Dropping Procedure DROP PROCEDURELargest; 11

12 Functions Syntax: [CREATE [OR REPLACE]] FUNCTION Fname [(p1, p2…)]RETURN datatype [IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS exception_handling] END [Fname]; / 12

13 Example CREATE FUNCTION PrintNameF RETURN BOOLEAN IS AddressVARCHAR2(50); BEGIN Address := ‘102 Main St, Frostburg MD 21532’; DBMS_OUTPUT.PUT_LINE(Address); RETURN true; ENDPrintNameF; / 13

14 Run a Function: DECLARE flagBOOLEAN; BEGIN Flag:=PrintNameF; IF flag THEN DBMS_OUTPUT.PUT_LINE(‘Function Printed Correctly’)); END IF; END; / 14

15 Function with Parameter CREATE OR REPLACE FUNCTION Print_B_DateF (V_ID NUMBER) RETURN CHAR IS V_B_DateDATE; foundCHAR(1):=‘F’; F_IDNUMBER:=V_ID; BEGIN SELECT B_Date INTOV_B_Date FROMEmployee WHEREID=F_ID; DBMS_OUTPUT.PUT_LINE(V_B_Date); found:=‘T’; RETURN found; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE(‘No Data Found’); END Print_B_DateF; / 15

16 Calling a Function: SELECT Print_B_DateF(102) FROM DUAL; 16

17 Dropping Function DROP FUNCTIONPrint_B_DateF; 17

18 Find the largest Value DECLARE –--p1, p2, p3, p4 largeNUMBER; 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

19 PACKAGES DBMS_OUTPUT.NEW_LINE; DBMS_OUTPUT.PUT_LINE ('Display This’); 19

20 PACKAGE HEADER (Specification) CREATE [ OR REPLACE] PACKAGE p_name IS |AS -- functions -- procedures -- variables -- constants -- exceptions -- cursors END [p_name ]; 20

21 EXAMPLE CREATE PACKAGE myPackage AS PROCEDURE PrintLarge (P1 NUMBER, p2 NUMBER); FUNCTION FindLarge (P1 NUMBER, P2 NUMBER) RETURN NUMBER; END myPackage; 21

22 PACKAGE BODY CREATE OR REPLACE PACKAGE BODY p_name IS|AS --package body End p_name; 22

23 EXAMPLE CREATE OR REPLACE PACKAGE BODY MyPackage AS PROCEDURE PrintLarge (P1 NUMBER, p2 NUMBER) AS BEGIN 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; BEGINIF p1> p2 THEN LARGE:=p1; ELSE LARGE:=p2; END IF; RETURN LARGE; END ; END myPackage; 23

24 Call Elements of Package: EXEC MyPackage. PrintLarge (32, 11); SELECT MyPackage. FindLarge( 22, 43) FROM DUAL; 24

25 EXAMPLE 2 CREATE OR REPLACE PACKAGE Employee_p AS FUNCTION 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

26 EXAMPLE 2 CREATE OR REPLACE PACKAGE BODY Employee_P AS FUNCTION E_Full_Name (Last_N Employee.L_name%TYPE, First_N Employee.F_name%TYPE) RETURN VARCHAR2 IS BEGIN RETURN Last_N || ‘, ‘ || First_N; END; 26

27 EXAMPLE 2 FUNCTION E_Name(f_id EMPLOYEE.id%TYPE) RETURN VARCHAR2 IS temp VARCHAR2(200); id EMPLOYEE.id%TYPE:=f_id; BEGIN SELECT (L_name || ‘ ‘ ||F_Name) INTO temp FROM employee WHERE employee.id = id; RETURN temp; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN TOO_MANY_ROWS THEN ….. END; END Employee_P; 27

28 CALL FUNCTIONS of PACKAGE SELECT Employee_p.E_NAME(107) FROMDUAL; 28

29 29 Subprogram Location USER_OBJECTS USER_SOURCE USER_ERRORS SELECTOBJECT_NAME, OBJECT_TYPE, STATUS FROMUSER_OBJECTS WHERE OBJECT_NAME = ‘PrintInfo’; OBJECT_NAMEOBJECT_TYPESTATUS SIMPLEPROCEDUREVALID

30 30 Subprogram Location SELECT TEXT FROM USER_SOURCE WHERE NAME = ‘PrintInfo’ AND TYPE= ‘PROCEDURE’ ORDER BY line; TEXT ----------------------------------------- CREATE OR REPLACE PROCEDURE PrintInfo AS ss NUMBER:=24; BEGIN DBMS_OUTPUT.PUT_LINE(ss); END PrintInfo;

31 31 Find Errors: SHOW ERRORS; -Line number of the error -Column number of the error -Text message of the error SELECT line, position, text FROM USER_ERRORS WHERE name=‘PrintInfo’ AND TYPE=‘PROCEDURE’;

32 Exception Handling CREATE FUNCTION Employee_P (f_id EMPLOYEE.id%TYPE) RETURN VARCHAR2 IS temp VARCHAR2(200); id EMPLOYEE.id%TYPE:=f_id; BEGIN SELECT (L_name || ‘ ‘ ||F_Name) INTO temp FROM employee WHERE employee.id = id; RETURN temp; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN TOO_MANY_ROWS THEN INSERT INTO errors VALUES (‘Error in statement ‘); RETURN NULL; END Employee_P; 32

33 33 DECLARE BEGIN EXCEPTION WHEN ZERO_DIVIDE THEN INSERT INTO table1(info) VALUES (info_data); COMMIT; WHEN OTHERS THEN ROLLBACK; END; Exception Handling

34 34 Exception Types: 1. Predefined Oracle Server errors: No declaration is needed ORA-01403 NO_DATA_FOUND ORA-01422 TOO_MANY_ROWS ORA-01476 ZERO_DIVIDE ORA-06500 STORAGE_ERROR ORA-06530 ACCESS_INTO_NULL ORA-06592 CASE_NOT_FOUND ORA-00001 DUP_VAL_ON_INDEX

35 35 Exception Types: 2.User_defined User determines an abnormal condition Declared in declaration section DECLARE e_TooManyNumbers EXCEPTION;

36 36 Raising USER_DEFINED Exceptions Example DECLARE e_TooManyNumbers EXCEPTION; V_NoStudentNUMBER(4); V_MaxStudentNUMBER(4); BEGIN SELECT Current_Students, Max_Students INTO V_NoStudent, V_MaxStudent FROM classes WHERE C_Num=455 AND Dept=‘ITEC’; IF V_NoStudent > V_MaxStudent THEN RAISE e_TooManyNumbers; END IF;

37 Chapter 18: Exception Handling37 EXCEPTION WHEN e_TooManyNumbers THEN INSERT INTO log_file (info) VALUES (‘ITEC455 has:‘ || V_NoStudent || ‘Max No is:’ || V_MaxStudent); END ;


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

Similar presentations


Ads by Google