Advertisement
jelledebock

EMPLOYEES_PER_COUNTRY

Dec 21st, 2014
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.63 KB | None | 0 0
  1. /*
  2. Gegevens van de werknemers per land en per locatie.
  3. */
  4. CREATE OR REPLACE PROCEDURE EMPLOYEES_PER_COUNTRY
  5. (
  6.   P_REGION_ID IN REGIONS.REGION_ID%TYPE  
  7. ) AS
  8.   -- Cursor going through every country
  9.   CURSOR country_cursor IS  SELECT * FROM HR.countries;
  10.   country_row country_cursor%ROWTYPE;
  11.  
  12.   -- Cursor to loop through every country's locations  
  13.   CURSOR location_cursor(country country_cursor%ROWTYPE) IS
  14.   SELECT * FROM HR.locations
  15.   WHERE country_id = country.country_id;
  16.   location_row location_cursor%ROWTYPE;
  17.  
  18.   -- Loop through every employee of a location
  19.   CURSOR emp_cursor(loc location_cursor%ROWTYPE) IS
  20.   SELECT e.first_name, e.last_name, d.department_name FROM HR.employees e
  21.   INNER JOIN departments d ON d.department_id = e.department_id
  22.   WHERE d.location_id = loc.location_id;
  23.   emp_row emp_cursor%ROWTYPE;
  24.  
  25.   -- Counter to count our employees per department
  26.   counter NUMBER;
  27. BEGIN
  28. --Outer loop : every country
  29. FOR country_row IN country_cursor
  30. LOOP
  31.   --Print the country name and it's code
  32.   DBMS_OUTPUT.put_line('Country= '||country_row.country_id || ' ' || country_row.country_name);
  33.   -- Inner loop: loop thru every countries location
  34.   FOR location_row IN location_cursor(country_row)
  35.   LOOP
  36.     --Print the location information (postal_code, city and state)
  37.     DBMS_OUTPUT.put_line('  Location= '||location_row.postal_code||' '||location_row.city||' '||location_row.state_province);
  38.     -- Final loop: go thru every employee of a location
  39.     counter := 0;
  40.     /* IF YOU WANT TO SAY THAT THERE AREN'T EMPLOYEES FOR A LOCATION DO THIS ...
  41.     *   select count(*) INTO counter FROM HR.employees e
  42.     *   INNER JOIN departments d ON d.department_id = e.department_id
  43.     *   WHERE d.location_id = location_row.location_id;
  44.     *   if counter > 0 then
  45.         *       for emp_row in emp_cursor(location_row) loop
  46.         *           dbms_output.put_line('   Employee= '||emp_row.first_name||' '||emp_row.last_name||'                
  47.     *               Department:'||emp_row.department_name);
  48.         *       end loop;
  49.         *       dbms_output.put_line('    --Total number of employees in '||location_row.city||' = '||counter);
  50.     *   else
  51.         *    dbms_output.put_line('    --No employees in our data');
  52.     *   end if;
  53.     *   end loop;
  54.     */
  55.     FOR emp_row IN emp_cursor(location_row) LOOP
  56.       counter := counter +1;
  57.      DBMS_OUTPUT.put_line('   Employee= '||emp_row.first_name||' '||emp_row.last_name||'  Department:'||emp_row.department_name);
  58.     END LOOP;
  59.     DBMS_OUTPUT.put_line('    --Total number of employees in '||location_row.city||' = '||counter);
  60.   END LOOP;
  61. END LOOP;
  62. END EMPLOYEES_PER_COUNTRY;
  63. /**** Test program ****/
  64. BEGIN
  65. employees_per_country(1);
  66. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement