PL/SQL - Packages:- Part - 15 Some Examples of Package - TopicsExpress



          

PL/SQL - Packages:- Part - 15 Some Examples of Package Features:- Consider the package below named emp_actions. The package spec declares the following types, items, and subprograms:- -> Types EmpRecTyp and DeptRecTyp -> Cursor desc_salary -> Exception invalid_salary -> Functions hire_employee and nth_highest_salary -> Procedures fire_employee and raise_salary After writing the package, you can develop applications that reference its types, call its subprograms, use its cursor, and raise its exception. When you create the package, it is stored in an Oracle database for general use. CREATE PACKAGE emp_actions AS /* Declare externally visible types, cursor, exception. */ TYPE EmpRecTyp IS RECORD (emp_id INT, salary REAL); TYPE DeptRecTyp IS RECORD (dept_id INT, location VARCHAR2); CURSOR desc_salary RETURN EmpRecTyp; invalid_salary EXCEPTION; /* Declare externally callable subprograms. */ FUNCTION hire_employee ( ename VARCHAR2, job VARCHAR2, mgr REAL, sal REAL, comm REAL, deptno REAL) RETURN INT; PROCEDURE fire_employee (emp_id INT); PROCEDURE raise_salary (emp_id INT, grade INT, amount REAL); FUNCTION nth_highest_salary (n INT) RETURN EmpRecTyp; END emp_actions; CREATE PACKAGE BODY emp_actions AS number_hired INT; -- visible only in this package /* Fully define cursor specified in package. */ CURSOR desc_salary RETURN EmpRecTyp IS SELECT empno, sal FROM emp ORDER BY sal DESC; /* Fully define subprograms specified in package. */ FUNCTION hire_employee ( ename VARCHAR2, job VARCHAR2, mgr REAL, sal REAL, comm REAL, deptno REAL) RETURN INT IS new_empno INT; BEGIN SELECT empno_seq.NEXTVAL INTO new_empno FROM dual; INSERT INTO emp VALUES (new_empno, ename, job, mgr, SYSDATE, sal, comm, deptno); number_hired := number_hired + 1; RETURN new_empno; END hire_employee; PROCEDURE fire_employee (emp_id INT) IS BEGIN DELETE FROM emp WHERE empno = emp_id; END fire_employee; /* Define local function, available only inside package. */ FUNCTION sal_ok (rank INT, salary REAL) RETURN BOOLEAN IS min_sal REAL; max_sal REAL; BEGIN SELECT losal, hisal INTO min_sal, max_sal FROM salgrade WHERE grade = rank; RETURN (salary >= min_sal) AND (salary
Posted on: Mon, 27 May 2013 14:04:34 +0000

Trending Topics



Recently Viewed Topics




© 2015