Plsql 使用PL/SQL的工资支出总额

Plsql 使用PL/SQL的工资支出总额,plsql,Plsql,我必须从Employee表中显示部门名称和部门的工资支出总额 EMPLOYEE: Column name Data type Constraints EMP_ID NUMBER(5) PK EMP_NAME VARCHAR2(25) NOT NULL SALARY NUMBER(10,2) DEPT VARCHAR2(25) EMP_ID EMP_NAME SALA

我必须从Employee表中显示部门名称和部门的工资支出总额

EMPLOYEE: 
Column name     Data type       Constraints
EMP_ID          NUMBER(5)       PK
EMP_NAME        VARCHAR2(25)    NOT NULL
SALARY          NUMBER(10,2)     
DEPT            VARCHAR2(25)

EMP_ID   EMP_NAME   SALARY  DEPT
101      Tom        54000   MECH
102      William    43000   CSE
103      John       34560   MECH
104      Smith      56000   CSE
105      Steve      23450   IT
Sample Output:
Department-wise salary expenditure:
IT department, total salary is  23450
CSE department, total salary is 99000
MECH department, total salary is 88560
我有这个密码-

set serveroutput on;
declare 
v_emp_rec employee%rowtype;
cursor op_cursor is select distinct(dept), sum(salary) from employee;
begin
dbms_output.put_line('Department-wise salary expenditure:');
open op_cursor;
loop
fetch op_cursor into v_emp_rec;
exit when op_cursor%notfound;
dbms_output.put_line(v_emp_rec.dept || ' department,' || ' total salary is ' || v_emp_rec.salary);
end loop;
close op_cursor;
end;
/

我不知道在那个地方使用
sum(salary)
是否正确,请查看此代码并提供帮助。提前谢谢

你应该按部门分组,然后计算总数。而不是:

cursor op_cursor is select distinct(dept), sum(salary) from employee;
做:

另外,我认为应该像这样循环光标:

FOR v_emp_rec IN op_cursor 
  LOOP
    dbms_output.put_line(v_emp_rec.dept || ' department,' || ' total salary is ' || v_emp_rec.salary);

  END LOOP;

你应该按部门分组,然后计算总数。而不是:

cursor op_cursor is select distinct(dept), sum(salary) from employee;
做:

另外,我认为应该像这样循环光标:

FOR v_emp_rec IN op_cursor 
  LOOP
    dbms_output.put_line(v_emp_rec.dept || ' department,' || ' total salary is ' || v_emp_rec.salary);

  END LOOP;

当我运行代码进行您所说的更改时,它在循环语句中显示一个错误,声明“SALARY必须声明”。我将其更改为添加“as SALARY”,它再次显示一个错误,但这次第一个
dbms\u输出
语句起作用。错误是
部门工资支出:第1行的declare*错误:ORA-06511:PL/SQL:cursor已打开ORA-06512:3行的ORA-06512:7行的
删除“open op_cursor;”行哦,我明白了。非常感谢您的时间和帮助!:)当我运行代码进行您所说的更改时,它在循环语句中显示一个错误,声明“SALARY必须声明”。我将其更改为添加“as SALARY”,它再次显示一个错误,但这次第一个
dbms\u输出
语句起作用。错误是
部门工资支出:第1行的declare*错误:ORA-06511:PL/SQL:cursor已打开ORA-06512:3行的ORA-06512:7行的
删除“open op_cursor;”行哦,我明白了。非常感谢您的时间和帮助!:)