无法弄清楚此pl / ql过程中的错误

时间:2016-10-27 07:18:50

标签: sql oracle plsql

我希望从数据库中获得平均分数,然后根据平均成绩对每个学生进行评分。

PROCEDURE avge (stu_id IN NUMBER, z OUT VARCHAR2, lv_avg OUT NUMBER) 
IS
BEGIN
  SELECT AVG(SCORE)
    into lv_avg
    FROM GRADE
   WHERE STUDENT_ID = stu_id;

  IF (lv_avg BETWEEN 90 AND 100) THEN
     z := 'A';
  ELSIF (lv_avg BETWEEN 80 AND 90) THEN
     z := 'B';
  ELSIF (lv_avg < 80) THEN
     z := 'C';
  END IF; 
END;

这些是弹出的错误:

ORA-06550: line 5, column 22:
PL/SQL: ORA-00904: "STU_ID": invalid identifier
ORA-06550: line 2, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 6, column 7:
PLS-00201: identifier 'LV_AVG' must be declared

3 个答案:

答案 0 :(得分:1)

也许你需要在pls / ql命令窗口运行它时添加create or replace

Create or replace PROCEDURE avge (stu_id IN NUMBER, z OUT VARCHAR2, lv_avg OUT NUMBER) 
IS
BEGIN
  SELECT AVG(SCORE)
    into lv_avg
    FROM GRADE
   WHERE STUDENT_ID = stu_id;

  IF (lv_avg BETWEEN 90 AND 100) THEN
     z := 'A';
  ELSIF (lv_avg BETWEEN 80 AND 90) THEN
     z := 'B';
  ELSIF (lv_avg < 80) THEN
     z := 'C';
  END IF; 
END;
/

答案 1 :(得分:0)

CREATE OR REPLACE PROCEDURE avge(stu_id IN INTEGER,z OUT CHAR,lv_avg OUT DECIMAL) AS
BEGIN
  SELECT AVG(score) INTO lv_avg FROM grade WHERE student_id=stu_id;
  IF (lv_avg BETWEEN 90 AND 100) THEN z:='A';
  ELSIF (lv_avg BETWEEN 80 AND 90) THEN z:='B';
  ELSE z:='C';
  END IF; 
END;
/

答案 2 :(得分:0)

PL / SQL不需要if个表达式括号,因为它们已经被then终止,所以代码中的那些是多余的。

case可以避免重复z :=。我还会选择一个比z更具描述性的参数名称 - 由于lv_avg既不是本地变量也不是与午餐券有任何关系,我不会在lv_前加上它。

除了into之外,您的所有关键字似乎都是大写的。那是为什么?

我会用这样的东西:

create or replace procedure avge
    ( p_student_id  in  grade.student_id%type
    , p_out_grade   out varchar2
    , p_out_avg     out grade.score%type )
is
begin
    select avg(score) into p_out_avg
    from   grade
    where  student_id = p_student_id;

    p_out_grade :=
        case
            when p_out_avg between 90 and 100 then 'A'
            when p_out_avg between 80 and  90 then 'B'
            when p_out_avg < 80 then 'C'
        end;
end avge;