sql - 数据库 PL/SQL

标签 sql oracle

我必须为学校布置作业,但出现两个错误:

Encountered the symbol "FETCH" when expecting on of the following: constant exception <an identifier> <a double-quoted delimited-identifier> table LONG_ double ref char time timestamp interval date binary national character nchar

Encountered the symbol "end-of-file" when expecting one of the following: end not pragma final instantiable order overriding static member constructor map

这是我的代码的链接: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;

最佳答案

在我看来对于初学者来说更容易使用的是游标循环,在这个项目中你会避免这种错误。语法如:

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

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

关于sql - 数据库 PL/SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37873637/

相关文章:

sql - 为什么我的 126 日期时间没有返回中间有 T 的内容?

mysql - 返回两个连续结果之间的 "diff"

sql - 避免在一个查询中多次使用同一个子查询

sql - 如果左联接返回任何行,则返回 '1'的更好方法?

sql - 将嵌套解码转换为等效的 CASE 语句(从 Oracle 转换到 PostgreSQL 需要)

java - 如何在jdbc中执行oracle过程

java - HSQL 中缺少用于测试的序列

SQL:获取每行的下一个日期值

php - 从数据库中删除逗号分隔列表中的值

java - Servlet 和资源文件