使用游标创建PL / SQL代码

时间:2013-11-27 03:31:48

标签: sql oracle oracle11g

我需要输出每位员工的总销售额,但收到错误

ORA-06550: line 7, column 1:
PLS-00103: Encountered the symbol "CURSOR" when expecting one of the following:

:= ( ; not null range default character
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

这是我的代码,我相信它与我的分组功能有关,但我显然已将这些库存标识到他们受尊敬的表格中。任何帮助将不胜感激

SET SERVEROUTPUT ON
Declare 

v_employee_fname employees2.first_name%TYPE;
v_employee_lname employees2.last_name%TYPE;
v_amount all_sales.amount%TYPE

CURSOR v_fullcount_cursor IS 
SELECT e2.first_name, e2.last_name, sum(alls.amount) as total_sales
FROM employees2 e2 join all_sales alls on e2.employee_id = alls.EMP_ID
GROUP BY e2.first_name, sum(alls.amount);
BEGIN 
OPEN v_fullcount_cursor;
LOOP
FETCH v_fullcount_cursor 
INTO v_employee_fname, v_employee_lname, v_amount;
EXIT WHEN v_fullcount_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_employee_fname ||' '|| v_employee_lname ||' total sales are $'|| 
v_amount);
END LOOP;
CLOSE v_fullcount_cursor;
END;
/

1 个答案:

答案 0 :(得分:2)

你错过了第6行的分号。

v_amount all_sales.amount%TYPE;

另一个问题是group by子句。您需要使用员工名字和姓氏对数据进行分组。试试这样,

SET SERVEROUTPUT ON
DECLARE 
     v_employee_fname employees2.first_name%TYPE;
     v_employee_lname employees2.last_name%TYPE;
     v_amount all_sales.amount%TYPE;

     CURSOR v_fullcount_cursor IS 
     SELECT e2.first_name, e2.last_name, sum(alls.amount) AS total_sales
     FROM   employees2 e2 JOIN all_sales alls ON e2.employee_id = alls.EMP_ID
     GROUP BY e2.first_name, e2.last_name;

     BEGIN 
          OPEN v_fullcount_cursor;
          LOOP
               FETCH v_fullcount_cursor 
               INTO v_employee_fname, v_employee_lname, v_amount;
               EXIT WHEN v_fullcount_cursor%NOTFOUND;
               DBMS_OUTPUT.PUT_LINE(v_employee_fname ||' '|| v_employee_lname ||' total sales are $'|| v_amount);
          END LOOP;
          CLOSE v_fullcount_cursor;
     END;
/