0% found this document useful (0 votes)
88 views

Lab7 1

This document defines an EMP_PKG package that contains procedures and functions for managing employee data. It includes procedures to add employees, get employee details, get employees by department, and show all employees. It also initializes an array of valid department IDs and checks that department IDs are valid when adding employees.

Uploaded by

Sonch Sajob
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Lab7 1

This document defines an EMP_PKG package that contains procedures and functions for managing employee data. It includes procedures to add employees, get employee details, get employees by department, and show all employees. It also initializes an array of valid department IDs and checks that department IDs are valid when adding employees.

Uploaded by

Sonch Sajob
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

CREATE OR REPLACE PACKAGE EMP_PKG IS TYPE emp_tabtype IS TABLE OF employees%ROWTYPE; PROCEDURE add_employee( first_name employees.first_name%TYPE, last_name employees.

last_name%TYPE, email employees.email%TYPE, job_id employees.job_id%TY PE DEFAULT 'SA_REP', manager_id employees.manager_id%TYPE DEFAULT 145, salary em ployees.salary%TYPE DEFAULT 1000, commission_pct employees.commission_pct%TYPE D EFAULT 0, department_id employees.department_id%TYPE DEFAULT 30); PROCEDURE add_employee( first_name employees.first_name%TYPE, last_name employees.last_name%TYPE, department_id employees.department_id%TYPE DEFAULT 30) ; FUNCTION get_employee(emp_id employees.job_id%TYPE) RETURN EMPLOYEES%RO WTYPE; FUNCTION get_employee(family_name employees.last_name%TYPE) RETURN EMPL OYEES%ROWTYPE; PROCEDURE get_employees(dept_id employees.department_id%TYPE); PROCEDURE init_departments; PROCEDURE print_employee(emprec employees%rowtype); PROCEDURE show_employees; FUNCTION valid_deptid(id IN departments.department_id%TYPE) RETURN BOOL EAN; END EMP_PKG; / CREATE OR REPLACE PACKAGE BODY EMP_PKG IS TYPE valid_department IS TABLE OF boolean INDEX BY BINARY_INTEGER; valid_departments valid_department; emp_table emp_tabtype; PROCEDURE add_employee( first_name employees.first_name%TYPE, last_name employee s.last_name%TYPE, email employees.email%TYPE, job_id employees.job_id%TYPE DEFAU LT 'SA_REP', manager_id employees.manager_id%TYPE DEFAULT 145, salary employees. salary%TYPE DEFAULT 1000, commission_pct employees.commission_pct%TYPE DEFAULT 0 , department_id employees.department_id%TYPE DEFAULT 30) IS BEGIN IF valid_deptid(department_id) THEN INSERT INTO employees(employee_id, first_name, last_name, email, job_id, manager_id, hire_date, salary, commission_pct, department_id) VALUES (employees_seq.NEXTVAL, first_name, last_name, email, job _id, manager_id, TRUNC(SYSDATE), salary, commission_pct, department_id); ELSE RAISE_APPLICATION_ERROR (-20204, 'Invalid department ID. Try aga in.'); END IF; END add_employee; PROCEDURE add_employee( first_name employees.first_name%TYPE, last_name employee s.last_name%TYPE, department_id employees.department_id%TYPE DEFAULT 30) IS email VARCHAR2(10); BEGIN email := UPPER(substr(first_name, 1, 1) || substr(last_name, 1, 7)); add_employee(first_name, last_name, email, department_id => department_i d); END add_employee; FUNCTION get_employee(emp_id employees.job_id%TYPE) RETURN EMPLOYEES%ROWTYPE IS emp EMPLOYEES%ROWTYPE; BEGIN SELECT *

INTO emp FROM employees WHERE employee_id = emp_id; RETURN emp; END get_employee; FUNCTION get_employee(family_name employees.last_name%TYPE) RETURN EMPLOYEES%RO WTYPE IS emp EMPLOYEES%ROWTYPE; BEGIN SELECT * INTO emp FROM employees WHERE last_name = family_name; RETURN emp; END get_employee; PROCEDURE get_employees(dept_id employees.department_id%type) IS BEGIN SELECT * BULK COLLECT INTO emp_table FROM EMPLOYEES WHERE department_id = dept_id; END; PROCEDURE print_employee(emprec employees%rowtype) IS BEGIN DBMS_OUTPUT.PUT_LINE(emprec.department_id ||' '|| emprec.employee_id||' '|| emprec.first_name||' '|| emprec.last_name||' '|| emprec.job_id||' '|| emprec .salary); END; PROCEDURE show_employees IS BEGIN IF emp_table IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('Employees in Package table'); FOR i IN 1 .. emp_table.COUNT LOOP print_employee(emp_table(i)); END LOOP; END IF; END show_employees; PROCEDURE init_departments IS i BINARY_INTEGER := 0; BEGIN FOR dep_record IN (SELECT * FROM departments) LOOP valid_departments(dep_record.department_id) := TRUE; i:= i+1; END LOOP; END init_departments; FUNCTION valid_deptid(id IN departments.department_id%TYPE) RETURN BOOLEAN IS count NUMBER; BEGIN RETURN valid_departments(id); EXCEPTION WHEN OTHERS THEN RETURN FALSE;

END valid_deptid; BEGIN init_departments; END EMP_PKG; /

You might also like