Advertisement
alrakis

07d Cursor Parameter

Jun 9th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 3.37 KB | None | 0 0
  1. SET serveroutput ON;
  2.  
  3. DECLARE
  4.  
  5.     --Declare the cursor
  6.     CURSOR Employees (SelectDept department.dnumber%TYPE) IS
  7.       SELECT *
  8.       FROM employee
  9.       WHERE (dno = SelectDept)
  10.     FOR UPDATE OF salary;
  11.  
  12.     --Declare holding variable
  13.     SelectDept  department.dnumber%TYPE;
  14.  
  15.     --Declare the row type associated to the CURSOR
  16.     EmpRecord   employee%ROWTYPE;
  17.  
  18.     --Declare variables for the pay
  19.     PayCut      employee.salary%TYPE := 0;
  20.     ReductionTotal  PayCut%TYPE := 0;
  21.    
  22.     --Declare dependent variables
  23.     DependentCount  INTEGER;
  24.     HasDependents   BOOLEAN := FALSE;
  25.    
  26.     --Declare work variables
  27.     HoursSum    works_on.hours%TYPE;
  28.     WorksHard   BOOLEAN := FALSE;
  29.    
  30.     --Declare misc variables
  31.     DayOfMonth  INTEGER;
  32.     TooLateInMonth  EXCEPTION;
  33.    
  34. BEGIN
  35.     --Retrieve the Day of Month
  36.     SELECT TO_CHAR(SYSDATE, 'DD')
  37.     INTO DayOfMonth
  38.     FROM DUAL;
  39.    
  40.     --Test the day
  41.     IF DayOfMonth > 25 THEN
  42.         RAISE TooLateInMonth;
  43.     END IF;
  44.  
  45.     --Open the CURSOR
  46.     IF NOT (Employees%ISOPEN) THEN
  47.    
  48.         --Retrieve department variable for the parameter
  49.         SELECT d.dnumber
  50.         INTO SelectDept
  51.         FROM department d
  52.               INNER JOIN dept_locations dl ON d.dnumber = dl.dnumber
  53.         WHERE dl.dlocation = 'Stafford'
  54.               AND ROWNUM <= 1;
  55.  
  56.         --Open the cursor passing the parameter
  57.         OPEN Employees (SelectDept);
  58.     END IF;
  59.    
  60.     --Begin the Indefinite LOOP
  61.     LOOP
  62.         --Retrieve the CURSOR data into the holding object
  63.         FETCH Employees INTO EmpRecord;
  64.        
  65.         --Test whether a record was found
  66.         EXIT WHEN Employees%NOTFOUND;
  67.  
  68.         --Calculate the Pay Cut
  69.         PayCut := EmpRecord.salary * .10;
  70.        
  71.         --Retrieve the number of dependents
  72.         SELECT COUNT(*)
  73.         INTO DependentCount
  74.         FROM dependent
  75.         WHERE essn = EmpRecord.ssn;
  76.        
  77.         --Set the boolean variable
  78.         HasDependents := (DependentCount > 0);
  79.        
  80.         --Retrieve the total number of hours for the current employee
  81.         SELECT SUM(hours)
  82.         INTO HoursSum
  83.         FROM works_on
  84.         WHERE essn = EmpRecord.ssn;
  85.        
  86.         --Set the Works Hard variable
  87.         WorksHard := (HoursSum > 40);
  88.        
  89.         --Test the Dependents and WorksHard variables
  90.         CASE
  91.             WHEN HasDependents THEN PayCut := PayCut - 100;
  92.             WHEN WorksHard THEN PayCut := PayCut - 50;
  93.             ELSE NULL;
  94.         END CASE;
  95.        
  96.         --Update the Employee record
  97.         UPDATE employee
  98.         SET salary = salary - PayCut
  99.         WHERE CURRENT OF Employees;
  100.        
  101.         --Ouput the detail to the user
  102.         DBMS_OUTPUT.put_line('Salary for ' || EmpRecord.LName ||
  103.                      'reduced by $' || PayCut);
  104.                      
  105.         --Calculate running totals
  106.         ReductionTotal := ReductionTotal + PayCut;
  107.         HasDependents := FALSE;
  108.         WorksHard := FALSE;
  109.  
  110.     END LOOP;
  111.  
  112.     --Commit the changes
  113.     COMMIT;
  114.  
  115.     --Close the CURSOR
  116.     IF Employees%ISOPEN THEN
  117.         CLOSE Employees;
  118.     END IF;
  119.    
  120.     --Output the running total
  121.     DBMS_OUTPUT.put_line('Total salary reduction: $' || ReductionTotal);
  122.    
  123. EXCEPTION
  124.     --Test for too late in month
  125.     WHEN TooLateInMonth THEN
  126.         DBMS_OUTPUT.put_line('No salary changes permitted after the 25th, program ending');
  127.    
  128.     --Test for generic error
  129.     WHEN OTHERS THEN
  130.         --Inform the user of the error
  131.         DBMS_OUTPUT.put_line('Unknown error, details below:');
  132.         DBMS_OUTPUT.put_line('Error code: ' || SQLCODE);
  133.         DBMS_OUTPUT.put_line('Error message: ' || SQLERRM);
  134.  
  135.         --Test whether the cursor is open
  136.         CASE EMPLOYEES%ISOPEN
  137.             WHEN TRUE THEN DBMS_OUTPUT.put_line('Cursor is currently open');
  138.             WHEN FALSE THEN DBMS_OUTPUT.put_line('Cursor is currently closed');
  139.         END CASE;
  140.  
  141. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement