Advertisement
jelledebock

Oefening1Package

Dec 18th, 2014
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 1.61 KB | None | 0 0
  1. CREATE OR REPLACE
  2. PACKAGE Oefening1Package AS
  3.   name_type VARCHAR2(50);
  4.   TYPE name_tabel IS TABLE OF name_type%TYPE;
  5.  
  6.   TYPE job_info IS RECORD (
  7.     job_id jobs.job_id%TYPE,
  8.     job_name jobs.job_title%TYPE,
  9.     names name_tabel
  10.   );  
  11.   FUNCTION geefWerknemers(p_job_id VARCHAR2) RETURN job_info;
  12. END Oefening1Package;
  13. /
  14. CREATE OR REPLACE
  15. PACKAGE BODY Oefening1Package AS
  16.   FUNCTION geefWerknemers(p_job_id VARCHAR2) RETURN job_info AS
  17.     job_i job_info;
  18.     counter INTEGER := 0;
  19.     CURSOR c1 IS
  20.       SELECT * FROM employees  
  21.       WHERE job_id = p_job_id;
  22.   BEGIN
  23.     /*Fetch job_id, job_name via simple select into */
  24.     SELECT job_id, job_title INTO job_i.job_id, job_i.job_name FROM jobs
  25.     WHERE job_id=p_job_id;
  26.     /* Get all employees and add them to job_info */
  27.     job_i.names := name_tabel();
  28.     FOR emp_rec IN c1
  29.     LOOP
  30.       counter:=counter+1;
  31.       job_i.names.extend;
  32.       job_i.names(counter) := emp_rec.first_name || ' ' || emp_rec.last_name;
  33.       EXIT WHEN c1%notfound;
  34.     END LOOP;
  35.     RETURN job_i;
  36.     END;
  37. END Oefening1Package;
  38. /
  39. /*------------Test program -------------------*/
  40. DECLARE
  41.    job_i HR.oefening1package.job_info;
  42.  
  43. BEGIN
  44.    -- Call the function
  45.    job_i := oefening1package.geefWerknemers('AD_PRES');
  46.    DBMS_OUTPUT.put_line('Number of employees with this job: ' || job_i.names.COUNT);
  47.    DBMS_OUTPUT.put_line(job_i.job_id);
  48.    DBMS_OUTPUT.put_line(job_i.job_name);
  49.    DBMS_OUTPUT.put_line('[');
  50.    FOR i IN 1..job_i.names.COUNT LOOP
  51.     DBMS_OUTPUT.put_line(job_i.names(i) || ', ' );
  52.    END LOOP;
  53.    DBMS_OUTPUT.put_line(']');
  54.    
  55. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement