Advertisement
jelledebock

Oefening2Package

Dec 18th, 2014
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 3.36 KB | None | 0 0
  1. CREATE OR REPLACE
  2. PACKAGE department_info AS
  3.   TYPE t_dep_loc IS RECORD(
  4.     region_name   regions.region_name%TYPE,
  5.     country_name  countries.country_name%TYPE,
  6.     state         locations.state_province%TYPE,
  7.     city          locations.city%TYPE
  8.   );
  9.   TYPE t_dep_mgr IS RECORD(
  10.     first_name    employees.first_name%TYPE,
  11.     last_name     employees.last_name%TYPE,
  12.     email         employees.email%TYPE,
  13.     phone_number  employees.phone_number%TYPE
  14.   );
  15.   /* Function returns a t_dep_loc record for a given department_id */
  16.   FUNCTION location_department(p_department_id departments.department_id%TYPE) RETURN t_dep_loc;
  17.   /* Fucntion returns a t_dep_mgr record for a given department_id */
  18.   FUNCTION manager_department(p_department_id departments.department_id%TYPE) RETURN t_dep_mgr;
  19. END department_info;
  20. /
  21. CREATE OR REPLACE
  22. PACKAGE BODY department_info IS
  23.   FUNCTION location_department(p_department_id departments.department_id%TYPE) RETURN t_dep_loc AS
  24.     dep t_dep_loc;
  25.   BEGIN
  26.     SELECT r.region_name, co.country_name, l.state_province, l.city INTO
  27.     dep.region_name, dep.country_name, dep.state, dep.city
  28.     FROM departments d
  29.     INNER JOIN locations l ON l.location_id = d.location_id
  30.     INNER JOIN countries co ON co.country_id = l.country_id
  31.     INNER JOIN regions r ON r.region_id = co.region_id
  32.     WHERE p_department_id = department_id;
  33.     RETURN dep;
  34.     EXCEPTION
  35.       WHEN NO_DATA_FOUND THEN
  36.         DBMS_OUTPUT.put_line('Error: no departments found for the given id');
  37.         RAISE;
  38.   END;
  39.   FUNCTION manager_department(p_department_id departments.department_id%TYPE) RETURN t_dep_mgr AS
  40.     mgr t_dep_mgr;
  41.   BEGIN
  42.     SELECT emp.first_name, emp.last_name, emp.email, emp.phone_number INTO mgr
  43.     FROM departments d
  44.     INNER JOIN employees emp ON d.manager_id=emp.employee_id
  45.     WHERE d.department_id = p_department_id;
  46.     RETURN mgr;
  47.     EXCEPTION
  48.       WHEN NO_DATA_FOUND THEN
  49.         DBMS_OUTPUT.put_line('Error: no departments found for the given id');
  50.         RAISE;
  51.   END;
  52. END;
  53. /*----TestApplicatie --------*/
  54. DECLARE
  55. v_department_id departments.department_id%TYPE;
  56. locatie department_info.t_dep_loc;
  57. manager department_info.t_dep_mgr;
  58. BEGIN
  59. -- De onderstaande regel zorgt ervoor dat je een dialoogvenster krijgt
  60. -- waarin je een department_id kan opgeven
  61. -- Om te debuggen maak je best gebruik van v_department_id := 50;
  62. v_department_id := '&Geef_de_departementid';
  63.  
  64. locatie := department_info.location_department(v_department_id);
  65. manager := department_info.manager_department(v_department_id);
  66.  
  67. DBMS_OUTPUT.put_line('Report of department with id: ' || v_department_id);
  68. DBMS_OUTPUT.put_line('-------------------------------------------------');
  69. DBMS_OUTPUT.put_line('Location: ');
  70. DBMS_OUTPUT.put_line('Regionaam: ' || locatie.region_name);
  71. DBMS_OUTPUT.put_line('Staat: ' || locatie.state);
  72. DBMS_OUTPUT.put_line('Stad: ' || locatie.city);
  73. DBMS_OUTPUT.put_line('');
  74. DBMS_OUTPUT.put_line('Manager: ');
  75. DBMS_OUTPUT.put_line('Voornaam: ' || manager.first_name);
  76. DBMS_OUTPUT.put_line('Achternaam: ' || manager.last_name);
  77. DBMS_OUTPUT.put_line('Email: ' || manager.email);
  78. DBMS_OUTPUT.put_line('Telefoon: ' || manager.phone_number);
  79. DBMS_OUTPUT.put_line('');
  80. DBMS_OUTPUT.put_line('-------------------------------------------------');
  81. EXCEPTION WHEN NO_DATA_FOUND THEN
  82. DBMS_OUTPUT.put_line('Foutje...');
  83. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement