Guest User

Untitled

a guest
Nov 23rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. -- Write a function that accepts
  2. -- department_id as a parameter
  3. -- and returns the count of employees
  4. -- in that department
  5.  
  6. -- e.g. count_employees(1001)
  7.  
  8. CREATE OR REPLACE FUNCTION hr.count_employees(deptid employees.department_id%TYPE) -- This is the parameter it takes
  9. RETURN NUMBER -- This is what it returns to the user
  10. IS
  11. emp_count NUMBER(6); -- I'll use this to store the answer
  12. BEGIN
  13. SELECT COUNT(employee_id) INTO emp_count
  14. FROM hr.employees
  15. WHERE department_id = deptid; -- Run the query for whatever deptid the user has requested
  16.  
  17. RETURN emp_count; -- Return the answer to the user
  18. END;
  19. -- Function code ends here
  20.  
  21. -- This is how to call a function for a simple test
  22. SET SERVEROUTPUT ON
  23. BEGIN
  24. DBMS_OUTPUT.PUT_LINE(hr.count_employees(50)); -- Count employees in deptid 50
  25. END;
Add Comment
Please, Sign In to add comment