Advertisement
jelledebock

[EXAMQUESTION] Firepeople

Jan 10th, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.09 KB | None | 0 0
  1. CREATE OR REPLACE
  2. PROCEDURE FIREPEOPLE(
  3. --Parameters : percentage of the total salary cost that need to be saved
  4. percentage IN NUMBER
  5. ) IS
  6.  salary_before NUMBER := 0;
  7.  salary_after NUMBER := 0;
  8.  invalid_percentage_exception EXCEPTION;
  9.  CURSOR dept_cursor IS SELECT * FROM departments;
  10.  CURSOR emp_cursor(dept NUMBER) IS
  11.     SELECT * FROM employees e
  12.     --join necessary tables
  13.     inner join jobs j ON j.job_id = e.job_id
  14.     WHERE e.department_id = dept
  15.     --order on jobid
  16.     ORDER BY j.min_salary ASC, j.max_salary ASC, e.hire_date DESC
  17.     FOR UPDATE NOWAIT;
  18.  emp_row emp_cursor%ROWTYPE;
  19. BEGIN
  20. --First check input parameter
  21. IF percentage < 0 OR percentage >1 THEN
  22.  RAISE invalid_percentage_exception;
  23. END IF;
  24. --Loop over all departments
  25. FOR v_dept_rec IN dept_cursor LOOP
  26.   SELECT SUM(salary) INTO salary_before FROM employees WHERE department_id = v_dept_rec.department_id;
  27.   salary_after := salary_before;
  28.   DBMS_OUTPUT.put_line('Department: ' || v_dept_rec.department_name);
  29.   DBMS_OUTPUT.put_line('Total before saving: '|| salary_before);
  30.   DBMS_OUTPUT.put_line('We want to reduce it to: '|| (salary_before-(percentage*salary_before)));
  31.   OPEN emp_cursor(v_dept_rec.department_id);
  32.   WHILE salary_after > (salary_before*(1-percentage)) LOOP
  33.     FETCH emp_cursor INTO emp_row;
  34.     EXIT WHEN emp_cursor%NOTFOUND;
  35.     DBMS_OUTPUT.put_line('-----Delete ' || emp_row.first_name || ' ' || emp_row.last_name);
  36.     DBMS_OUTPUT.put_line('-----His/her salary was ' || emp_row.salary);
  37.     DELETE FROM employees WHERE CURRENT OF emp_cursor;
  38.     salary_after := salary_after-emp_row.salary;
  39.   END LOOP;
  40.   CLOSE emp_cursor;
  41.   DBMS_OUTPUT.put_line('Total after saving: '|| salary_after);
  42.   DBMS_OUTPUT.put_line('Percentage saved: '|| ROUND((1-salary_after/salary_before)*100,2));
  43.   DBMS_OUTPUT.put_line('***********************************************************************');
  44. END LOOP;
  45. EXCEPTION
  46.   WHEN invalid_percentage_exception THEN
  47.     DBMS_OUTPUT.put_line('Please enter an amount between 0 and 1');
  48.   WHEN OTHERS THEN
  49.     DBMS_OUTPUT.put_line('Unknown exception');
  50. END FIREPEOPLE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement