这是我的程序代码。我创建了两个表作为Stud_Marks和Result,并在其中添加了一些数据...然后创建了此过程。在运行时,它显示“对象SYS.FIND_CLASS无效”的错误。
SQL> create or replace procedure find_class(roll IN number) IS
2 tm number (10);
3 begin
4 select total_marks into tm from stud_marks where name IN(select name from result where roll=roll_no);
5 if(tm<=1500 and tm>=990)then
6 update result set class='distin' where roll_no=roll;
7 elseif(tm>900 and tm<989)then
8 update result set class='first' where roll_no=roll;
9 elseif(tm<=899 and tm>=825)then
10 update result set class='second' where roll_no=roll;
11 end if;
12 exception when no_data_found then
13 dbms_output.put_line('roll no is not matched with the entry');
14 end;
15 /
[enter image description here][1]
Warning: Procedure created with compilation errors
。
SQL> declare
2 r number(10);
3 begin
4 r:=&roll_no;
5 find_class(r);
6 end;
7 /
Enter value for roll_no: 1
old 4: r:=&roll_no;
new 4: r:=1;
find_class(r);
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00905: object SYS.FIND_CLASS is invalid
ORA-06550: line 5, column 1:
PL/SQL: Statement ignored
那我发现find_class(r)出错了。为什么?
答案 0 :(得分:1)
在调用该过程之前,必须确保它是有效的。您不是,因此Oracle会引发错误。
创建后,会显示一条消息
警告:程序创建时出现编译错误
您应该已经通过运行命令检查了什么问题
show err
我重新格式化了您发布的代码-了解如何更容易阅读:
create or replace procedure find_class(roll IN number) IS
tm number (10);
begin
select total_marks
into tm
from stud_marks
where name IN (select name
from result
where roll = roll_no
);
if (tm <= 1500 and tm >= 990)then
update result set
class = 'distin'
where roll_no = roll;
elsif (tm > 900 and tm < 989) then --> ELSIF, not ELSEIF
update result set
class = 'first'
where roll_no = roll;
elsif (tm <= 899 and tm >= 825) then --> ELSIF, not ELSEIF
update result set
class = 'second'
where roll_no = roll;
end if;
exception when no_data_found then
dbms_output.put_line('roll no is not matched with the entry');
end;
显然,您使用了ELSEIF
,而应该使用ELSIF
。我不知道这是否是唯一的错误,因为我没有您的桌子-您会发现的。如有必要,请不要忘记show err
!
因此,一旦创建完成没有错误,您就可以调用它并查看其作用。
P.S。忘了提及:为什么将其连接为SYS
?该用户拥有数据库,应不用于开发或教育目的。您应该使用预定义用户之一(例如Scott
或HR
)或创建自己的用户。别管SYS
。