创建一个存储过程,接受并计算该部门的总工资,并显示部门#&总薪水

时间:2015-07-04 05:21:19

标签: oracle oracle11g

我在这里做错了什么?

  

创建一个接受部门编号的程序,计算该部门的基本工资总额,并显示部门编号和基本工资总额。

CREATE OR REPLACE PROCEDURE DEPT_TOTAL_SALARY AS 
BEGIN
   SELECT DEPARTMENTS.DEPARTMENTS_ID, SUM(SALARY) AS TOTAL_SALARIES
   FROM EMPLOYEES, DEPARTMENTS
   WHERE EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
   DBMS_OUTPUT.PUT_LINE (‘*** TOTAL SALARY CALCULATED ***’);
END;
/ 
  

第6行出错:PL / SQL:ORA-00933:SQL命令未正确结束

     
      
  1. 来自员工,部门
  2.   
  3. WHERE EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
  4.   
  5. DBMS_OUTPUT.PUT_LINE(' *总计计算* ');
  6.   
  7. END;
  8.   
  9. /
  10.   
Table:  EMPLOYEES                           
    EMPLOYEE_ID EMPLOYEE_NAME   JOB_ID  JOB_DESCRIPTION HOD HIREDATE    SALARY  DEPARTMENT_ID
              1        SMITH        J1         CLERK    5   17-Dec-07    5,800.00   20
              2        ALLEN        J2      SLAESMAN    3   20-Feb-08    7,600.00   30
              3         WARD        J2      SALESMAN    5   22-Feb-08    8,250.00   30
              4        JONES        J3       MANAGER    7   2-Apr-08     4,900.00   20
              5         JACK        J2      SALESMAN    7   28-Sep-08    6,700.00   10
              6        BLAKE        J3       MANAGER    7   1-May-08     12,850.00  30
              7        CLARK        J3       MANAGER   NULL 9-Jun-08     22,450.00  10
              8        SCOTT        J4       ANALYST    5   9-Dec-08     13,000.00  20
              9       TURNER        J2      SALESMAN    3   8-Sep-07     5,500.00   30
             10        ADAMS        J1         CLERK    5   12-Jan-07    9,100.00   20
Table:  DEPARTMENTS     
DEPARTMENT_ID   DEPARTMENT_NAME LOCATION
          10    ACCOUNTING  NEW YORK
          20    PRODUCTION  MINNESOTA
          50    SALES   CHICAGO
          40    OPERATIONS  BOSTON
  

如果DeptNum为30,下面的查询代码会执行以下操作吗?如果没有,我如何根据上面的EMPLOYEES表编写代码来计算每个部门的所有工资?

 DeptNum    Total_Salary
--------    ------------
      30     34,200.00 
CREATE OR REPLACE PROCEDURE Dept_Total_Salary (deptNum NUMBER)
        AS
          total_salary NUMBER(12, 2);
        BEGIN
          SELECT SUM(Salary)
            INTO total_salary
            FROM Employees
            WHERE Department_ID = deptNum;
          DBMS_OUTPUT.PUT_LINE('Department ' || deptNum || ' salary is ' || total_salary);
        END;

3 个答案:

答案 0 :(得分:3)

薪水和部门编号都在EMPLOYEES表中,因此无需在查询中包含DEPARTMENTS表。

您的查询中的错误是:

  1. 您不接受部门编号作为存储过程的参数。

  2. 当您不需要时,您将包含DEPARTMENTS表。

  3. 您没有使用WHERE条款按部门编号进行过滤。

  4. 您需要在变量中选择总薪水才能打印出来。

  5. SELECT查询后您需要一个分号。

  6. 尝试这样的事情:

    CREATE OR REPLACE PROCEDURE Dept_Total_Salary (deptNum NUMBER)
    AS
      total_salary NUMBER(12, 2);
    BEGIN
      SELECT SUM(Salary)
        INTO total_salary
        FROM Employees
        WHERE Department_ID = deptNum;
      DBMS_OUTPUT.PUT_LINE('Department ' || deptNum || ' salary is ' || total_salary);
    END;
    

    编辑:在END之后忘记了分号,在我指出你错过了分号之后 - 哎哟!

答案 1 :(得分:0)

  

接受部门编号

你做错的第一件事就是忽略了这方面的要求。您需要编写一个带有部门编号的过程,并使用where子句来限制从表中检索的内容。

答案 2 :(得分:0)

这是我解决这个问题的方式。

CREATE TABLE proTable -- create new table to store sum of sal for each dept
    (deptno NUMBER,
    sumSal NUMBER);

CREATE OR REPLACE PROCEDURE display_dept_sal_sum (v_deptno NUMBER)
AS 
BEGIN
        INSERT INTO proTable (deptno,SumSal)
        SELECT v_deptno,SUM(sal) 
        FROM employees 
        WHERE deptno = v_deptno;
END;

EXECUTE  display_dept_sal_sum(40);
SELECT * FROM proTable;