隐式游标循环中DBMS输出的PL / SQL过程

时间:2014-03-01 00:56:03

标签: loops plsql cursor procedure implicit

我正在做家庭作业,要求根据商标进行字母等级更新。两者都必须通过程序完成。第一个使用隐式游标,第二个使用显式游标。我的显式游标过程完美无缺。我的隐式游标有两个问题。第一个,它似乎没有为50的标记更新。第二,当显示输出时,它显示学生ID但不显示更新的成绩。有没有修复这些的想法?以下是我到目前为止:

CREATE OR REPLACE PROCEDURE comp_grade_s IS
  CURSOR cur_class IS
    SELECT studid, marks, grade
    FROM class
  FOR UPDATE NOWAIT;
  TYPE type_class IS RECORD
    (studid class.studid%TYPE,
    marks class.marks%TYPE,
    grade class.marks%TYPE);
  rec_class type_class;
  lv_grade_txt class.grade%TYPE;
BEGIN
  OPEN cur_class;
  LOOP
    FETCH cur_class INTO rec_class;
      EXIT WHEN cur_class%NOTFOUND;
      UPDATE class
        SET grade = 'F'
        WHERE marks < 50;
      UPDATE class
        SET grade = 'D'
        WHERE marks >= 50 AND marks < 60;
      UPDATE class
        SET grade = 'C'
        WHERE marks >= 60 AND marks < 70;
      UPDATE class
        SET grade = 'B'
        WHERE marks >= 70 AND marks < 80;
      UPDATE class
        SET grade = 'A'
        WHERE marks >= 80;
      UPDATE class
        SET grade = lv_grade_txt
        WHERE CURRENT OF cur_class;
      DBMS_OUTPUT.PUT_LINE('Student ID: ' || rec_class.studid || ' gets grade ' ||
        lv_grade_txt); 
  END LOOP;
  CLOSE cur_class;
END;

预先感谢您的任何帮助, 艾米丽

1 个答案:

答案 0 :(得分:0)

尝试这个我不知道你为什么不试图从

中打印dbms_output中的等级

您已定义的记录

创建或替换PROCEDURE comp_grade_s IS

CURSOR cur_class IS

SELECT studid, marks, grade

FROM class

FOR FORDATE NOWAIT;

TYPE type_class IS RECORD

(studid class.studid%TYPE,

marks class.marks%TYPE,

grade class.grade%TYPE);

rec_class type_class;

BEGIN

OPEN cur_class;

LOOP

FETCH cur_class INTO rec_class;

  EXIT WHEN cur_class%NOTFOUND;

  UPDATE class

    SET grade = 'F'

    WHERE marks < 50;

  UPDATE class

    SET grade = 'D'

    WHERE marks >= 50 AND marks < 60;

  UPDATE class

    SET grade = 'C'

    WHERE marks >= 60 AND marks < 70;

  UPDATE class

    SET grade = 'B'

    WHERE marks >= 70 AND marks < 80;

  UPDATE class

    SET grade = 'A'

    WHERE marks >= 80;

  UPDATE class

    SET grade = lv_grade_txt

    WHERE CURRENT OF cur_class;

  DBMS_OUTPUT.PUT_LINE('Student ID: ' || rec_class.studid || ' gets grade ' ||

    rec_class.grade); 

END LOOP;

关闭cur_class;

END;