Oracle 11g包体

时间:2018-05-04 19:42:37

标签: plsql oracle11g package

我正在为课程编写工作表,并提出了有关创建包的问题。创建在同一链接上显示的规范很容易。它是第一次运行。但包体让我难过。我花了近两天的时间来修复一组只有另一组才出现。

CREATE OR REPLACE PACKAGE BODY dept_pkg AS

  PROCEDURE new_dept (
    p_id          departments.department_id%TYPE,
    p_dept_name   departments.department_name%TYPE,
    p_manager     departments.manager_id%TYPE,
    p_loc         departments.location_id%TYPE
  )
    AS
  BEGIN
    INSERT INTO departments (
      dept_id,
      department_name,
      manager_id,
      location
    ) VALUES (
      p_id,
      p_dept_name,
      p_manager,
      p_loc
    );

  END;

  FUNCTION get_avg_sal (
    p_dept_id departments.department_id%TYPE
  ) RETURN NUMBER IS
    v_avg_sal   NUMBER;
  BEGIN
    SELECT AVG(salary) INTO
      v_avg_sal
    FROM employees
    WHERE department_id = p_dept_id;

    return(v_avg_sal);
  END get_avg_sal;

  FUNCTION get_department_count (
    p_dept_id departments.department_id%TYPE
  ) RETURN NUMBER IS
    v_emp_num   NUMBER(7);
  BEGIN
    SELECT COUNT(employee_id) INTO
      v_emp_num
    FROM employees
    WHERE p_dept_id = employees.department_id;

    return(v_emp_num);
  END;

  PROCEDURE delete_department (
    p_dept_id departments.department_id%TYPE
  ) AS
    v_num_employees   NUMBER;
  BEGIN
    SELECT get_deptartment_count(p_dept_id) INTO
      v_num_employees
    FROM dual;

    IF
      v_num_employees = 0
    THEN
      DELETE FROM employees WHERE p_dept_id = department_id;

    ELSE
      dbms_output.put_line('the department cannot be deleted because there are employees assigned to that department');
    END IF;

  END delete_department;

END dept_pkg;

0 个答案:

没有答案