Successfully reported this slideshow.
Your SlideShare is downloading. ×

Oracle db subprograms

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Packages in PL/SQL
Packages in PL/SQL
Loading in …3
×

Check these out next

1 of 24 Ad

More Related Content

Advertisement
Advertisement

Recently uploaded (20)

Oracle db subprograms

  1. 1. Oracle DB Subprograms Simon Huang simon581923@gmail.com
  2. 2. Agenda • PL/SQL Units • Anonymous Block • Stroed PL/SQL Units • PL/SQL Packages
  3. 3. 參考文件 • Oracle Database Documentation Library  http://www.oracle.com/pls/db112/homepage • E25519  Oracle Database PL/SQL Language Reference 11g Release 2 (11.2) • E41502  Oracle Database Advanced Application Developer's Guide 11g Release 2 (11.2)
  4. 4. PL/SQL Units
  5. 5. PL/SQL Units • PL/SQL is a modern, block-structured programming language. PL/SQL provides procedural constructs, such as loops and conditional statements, that are not available in standard SQL. • You can directly enter SQL data manipulation language (DML) statements inside PL/SQL blocks, and you can use subprograms supplied by Oracle to perform data definition language (DDL) statements. • PL/SQL Units include  Anonymous Blocks  Stored PL/SQL Units  Triggers
  6. 6. Anonymous Block
  7. 7. Anonymous block • An anonymous block is a PL/SQL unit that has no name. • An anonymous block consists of  An optional declarative part  An executable part  And one or more optional exception handlers. • The declarative part declares  PL/SQL variables  Exceptions handlers  Cursors • The executable part contains  PL/SQL code  SQL statements  And can contain nested blocks. • Exception handlers contain code that is invoked when the exception is raised, either as a predefined PL/SQL exception (such as NO_DATA_FOUND or ZERO_DIVIDE) or as an exception that you define.
  8. 8. Anonymous block Example - Simple DECLARE last_name VARCHAR2(10); cursor c1 IS SELECT LAST_NAME FROM EMPLOYEES WHERE DEPARTMENT_ID = 20 ORDER BY LAST_NAME; BEGIN OPEN c1; LOOP FETCH c1 INTO last_name; EXIT WHEN c1%NOTFOUND; DBMS_OUTPUT.PUT_LINE(last_name); END LOOP; END;
  9. 9. Anonymous block Example - Exception Handler DECLARE Emp_number INTEGER := 9999; Emp_name VARCHAR2(10); BEGIN SELECT LAST_NAME INTO Emp_name FROM EMPLOYEES WHERE EMPLOYEE_ID = Emp_number; DBMS_OUTPUT.PUT_LINE('Employee name is ' || Emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No such employee: ' || Emp_number); END;
  10. 10. Anonymous block Example - User-Defined Exception DECLARE Emp_name VARCHAR2(10); Emp_number INTEGER; Empno_out_of_range EXCEPTION; BEGIN Emp_number := 10001; IF Emp_number > 9999 OR Emp_number < 1000 THEN RAISE Empno_out_of_range; ELSE SELECT LAST_NAME INTO Emp_name FROM EMPLOYEES WHERE EMPLOYEE_ID = Emp_number; DBMS_OUTPUT.PUT_LINE('Employee name is ' || Emp_name); END IF; EXCEPTION WHEN Empno_out_of_range THEN DBMS_OUTPUT.PUT_LINE('Employee number ' || Emp_number || ' is out of range.'); END;
  11. 11. Stored PL/SQL Units
  12. 12. Stored PL/SQL Units • A stored PL/SQL unit is a subprogram (procedure or function) or package that:  Has a name.  Can take parameters, and can return values.  Is stored in the data dictionary.  Can be invoked by many users. • If a subprogram belongs to a package, it is called a package subprogram; if not, it is called a standalone subprogram.
  13. 13. Stored Procedure with Parameters CREATE OR REPLACE PROCEDURE get_emp_names ( dept_num IN NUMBER ) IS emp_name VARCHAR2(10); CURSOR c1 (dept_num NUMBER) IS SELECT LAST_NAME FROM EMPLOYEES WHERE DEPARTMENT_ID = dept_num; BEGIN OPEN c1(dept_num); LOOP FETCH c1 INTO emp_name; EXIT WHEN C1%NOTFOUND; DBMS_OUTPUT.PUT_LINE(emp_name); END LOOP; CLOSE c1; END;
  14. 14. Stored Procedure with %TYPE and %ROWTYPE CREATE OR REPLACE PROCEDURE get_emp_rec ( emp_number IN EMPLOYEES.EMPLOYEE_ID%TYPE, emp_info OUT EMPLOYEES%ROWTYPE) IS BEGIN SELECT * INTO emp_info FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_number; END;
  15. 15. Function CREATE FUNCTION get_bal(acc_no IN NUMBER) RETURN NUMBER IS acc_bal NUMBER(11,2); BEGIN SELECT order_total INTO acc_bal FROM orders WHERE customer_id = acc_no; RETURN(acc_bal); END;
  16. 16. 執行Procedure & Function • EXEC get_emp_names(10); • VARIABLE emp_info EMPLOYEES%ROWTYPE; • EXEC get_emp_rec( 7369, : emp_info); • SELECT get_bal(165) FROM DUAL;
  17. 17. PL/SQL Packages
  18. 18. PL/SQL Packages - Concept • A package is a collection of related program objects (for example, subprogram, variables, constants, cursors, and exceptions) stored as a unit in the database. • Packages is an alternative to creating subprograms as standalone schema objects. • Packages have many advantages over standalone subprograms. For example, they:  Let you organize your application development more efficiently.  Let you grant privileges more efficiently.  Let you modify package objects without recompiling dependent schema objects.  Enable Oracle Database to read multiple package objects into memory at once.  Can contain global variables and cursors that are available to all subprograms in the package.  Let you overload subprograms. Overloading a subprogram means creating multiple subprograms with the same name in the same package, each taking arguments of different number or data type.
  19. 19. PL/SQL Packages - Concept • A package include two parts: the specification part and the body of a package. • The specification part of a package declares the public types, variables, constants, and subprograms that are visible outside the immediate scope of the package. • The body of a package defines both the objects declared in the specification and private objects that are not visible to applications outside the package.
  20. 20. Package Specification - Example -- Package specification: CREATE or REPLACE PACKAGE employee_management IS FUNCTION hire_emp ( firstname VARCHAR2, lastname VARCHAR2, email VARCHAR2, phone VARCHAR2, hiredate DATE, job VARCHAR2, sal NUMBER, comm NUMBER, mgr NUMBER, deptno NUMBER ) RETURN NUMBER; PROCEDURE fire_emp( emp_id IN NUMBER ); PROCEDURE sal_raise ( emp_id IN NUMBER, sal_incr IN NUMBER ); END employee_management;
  21. 21. Package Body – Example I -- Package body: CREATE or REPLACE PACKAGE BODY employee_management IS FUNCTION hire_emp ( firstname VARCHAR2, lastname VARCHAR2, email VARCHAR2, phone VARCHAR2, hiredate DATE, job VARCHAR2, sal NUMBER, comm NUMBER, mgr NUMBER, deptno NUMBER ) RETURN NUMBER IS new_empno NUMBER(10); BEGIN new_empno := emp_sequence.NEXTVAL; INSERT INTO EMPLOYEES ( employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id) VALUES ( new_empno, firstname, lastname, email, phone, hiredate, job, sal, comm, mgr, deptno); RETURN (new_empno); END hire_emp;
  22. 22. Package Body – Example II PROCEDURE fire_emp ( emp_id IN NUMBER) IS BEGIN DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_id; IF SQL%NOTFOUND THEN raise_application_error( -20011, 'Invalid Employee Number: ‘ || TO_CHAR(Emp_id)); END IF; END fire_emp; PROCEDURE sal_raise ( emp_id IN NUMBER, sal_incr IN NUMBER) IS BEGIN UPDATE EMPLOYEES SET SALARY = SALARY + sal_incr WHERE EMPLOYEE_ID = emp_id; IF SQL%NOTFOUND THEN raise_application_error( -20011, 'Invalid Employee Number: ‘ || TO_CHAR(Emp_id)); END IF; END sal_raise; END employee_management;
  23. 23. Invoke package procedures DECLARE empno NUMBER(6); sal NUMBER(6); temp NUMBER(6); BEGIN empno := employee_management.hire_emp( 'John', 'Doe', 'john.doe@company.com', '555-0100', '20-SEP-07', 'ST_CLERK', 2500, 0, 100, 20); DBMS_OUTPUT.PUT_LINE( 'New employee ID is ' || TO_CHAR(empno)); END;
  24. 24. Q & A

×