如何在oracle存储过程可调用语句中修复这些错误?

时间:2017-03-09 14:11:09

标签: java oracle stored-procedures jdbc

create or replace procedure updateStudentResult(id int)
as
m1 number(4);
m2 number(4);
m3 number(4);
tot number(4);
avg float;
grd char(1);
begin
select marks1,marks2,marks3 into m1,m2,m3 from students where sid=id;
tot:=m1+m2+m3;
avg:=tot/3;
if(avg>=85.0) then
grd:='A';
elsif(avg>=65.0) then
grd:='B';
elsif(avg>=50.0) then
grd:='C';
else
grd:='D';
end if;
update students set total=tot,average=avg,grade=grd where sid=id;
end;
/

3 个答案:

答案 0 :(得分:0)

avg是一个保留字(它是一个函数)。您不能在plsql中将其用作变量名。使用另一个变量名称计算平均值。

create or replace procedure updatestudentresult(id int) as
   m1  number(4);
   m2  number(4);
   m3  number(4);
   tot number(4);
   l_avg float;
   grd char(1);
begin
   select marks1
         ,marks2
         ,marks3
     into m1
         ,m2
         ,m3
     from students
     where sid = id;

   tot := m1 + m2 + m3;

   l_avg := tot / 3;

   if l_avg > 85.0
   then
      grd := 'A';
   elsif (l_avg >= 65.0)
   then
      grd := 'B';
   elsif (l_avg >= 50.0)
   then
      grd := 'C';
   else
      grd := 'D';
   end if;

   update students
      set total   = tot
         ,average = l_avg
         ,grade   = grd
    where sid = id;
end;

答案 1 :(得分:0)

所有程序代码和初始选择都是不必要的。这就足够了;

CREATE OR REPLACE PROCEDURE updatestudentresult(id INT) AS

BEGIN
  UPDATE students
     SET total = mark1+mark2+mark3,
     average = (mark1+mark2+mark3)/3,
     grd = CASE
           WHEN (mark1+mark2+mark3)/3 > 85 THEN 'A'
           WHEN (mark1+mark2+mark3)/3 > 65 THEN 'B'
           WHEN (mark1+mark2+mark3)/3 > 50 THEN 'C'
           ELSE 'D'
           END
   WHERE sid = id;
END;

答案 2 :(得分:0)

仅仅因为你可以在pl / sql中执行此操作,这可能会更好地作为更新语句。这里没有判断数据模型,只是使用你拥有的东西。

update students
  set total = mark1+mark2+mark3,
        avg = (mark1+mark2+mark3)/3,
        grd = case 
            WHEN (mark1+mark2+mark3)/3 > 85.0 then 'A'
            when (mark1+mark2+mark3)/3 > 65.0 then 'B'
            when (mark1+mark2+mark3)/3 > 50.0 then 'C'
            else 'D' end 
where  sid = :sid;