数据库PL / SQL

时间:2016-06-17 05:19:37

标签: sql oracle

我必须为学校做作业,但我得到两个错误:

  

遇到符号&#34; FETCH&#34;当期待以下内容:   常量异常<an identifier> <a double-quoted delimited-identifier>表LONG_ double ref char时间戳   间隔日期二进制国家字符nchar

  

遇到符号&#34;文件结束&#34;当期待其中一个   以下:结束不是pragma最终可实例化的命令覆盖静态   成员构造函数映射

以下是我的代码的链接:http://pastebin.com/h4JN9YQY

CREATE OR REPLACE PROCEDURE generate_bonus
AS

cursor student_info is
    select distinct students.id,
  events.begindatetime,
    events.enddatetime,
    count(items.number_of_coupons) as coupons_collected,
    events.type from students
    join applies on applies.students_id = students.id
    join schedules on schedules.id = applies.schedules_id
    join events on events.id = schedules.events_id
    join orders on orders.students_id = students.id
    join orderitems on orderitems.orders_id = orders.id
    join items on items.id = orderitems.items_id
    join bars on bars.id = orders.bars_id
    where applies.status = 'PLANNED'
    and orderitems."NUMBER" is not null
    and bars.name is not null
    group by students.id, events.begindatetime, events.enddatetime, events.type
    order by students.id;

BEGIN

    DECLARE
    s_id integer(256);
    s_beginDate date;
    s_endDate date;
    s_noCoupons number(256);
    s_eventType varchar2(256);
    s_workedHours number(24) := 8;
    calculated_bonus number(256);
    count_rows integer(256);

    OPEN student_info;

        LOOP

        FETCH student_info into s_id, s_beginDate, s_endDate, s_noCoupons, s_eventType;

            Select count(*) into count_rows from student_bonus where students_id = s_id and rownum <= 1;

      EXIT WHEN count_rows = 1;

            IF (s_eventType = 'ROUGH') THEN
                calculated_bonus := s_workedHours * (s_workedHours / 100 * 7) * s_noCoupons;

                INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);

                calculated_bonus := 0;

            ELSIF (s_eventType = 'NORMAL') THEN
                calculated_bonus := s_workedHours * (s_workedHours / 100 * 4) * s_noCoupons;

                INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);

                calculated_bonus := 0;

            ELSE
                calculated_bonus := s_workedHours * (s_workedHours / 100 * 2) * s_noCoupons;

                INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);

                calculated_bonus := 0;
            END IF;

        END LOOP;

    CLOSE student_info;

END generate_bonus;

3 个答案:

答案 0 :(得分:0)

把这一行:

EXIT WHEN student_info%NOTFOUND;

这一行之后:

FETCH student_info into s_id, s_beginDate, s_endDate, s_noCoupons, s_eventType;

您已到达光标的末尾,但没有代码告诉它退出。

答案 1 :(得分:0)

光标应按以下方式执行步骤..

Open Cursor
LOOP
 FETCH cursor
 EXIT Cursor condtion

{--
Your rest of the code here
--}

end loop;

答案 2 :(得分:0)

在我看来,青少年更容易使用的是游标循环,在这个项目中你将避免这种错误。语法如:

FOR row_variable IN cursor LOOP
dbms_output.put_line(row_variable.id);
END LOOP;

row_variable从每个光标行中保存valeus,您可以使用“。”轻松访问它。 (点)运算符,如row_variable.id 使用游标循环可以避免获取数据时出现问题,注意打开/关闭游标并担心光标空间外的输出。 循环将精确地循环游标指向的项目数,就像每个循环一样。