oracle的存储过程:游标,case ,if
题目:创建一个存储过程,以学生的学号为参数,修改该学生的成绩。若该学生的课程编号为c003则成绩加5分,若课程的编号为c004则成绩添加10分。
首先我创建了一个表为SC,表中的有三个字段:sno(学号)cno (课程编号) ,score(成绩)
create or replace procedure cj(v_sno SC.sno%type) is--创建存储过程
begin
for v_cno in(select * from SC where sno=v_sno)loop--以输入的学号进行循环,选择一条数据作为游标
if v_cno.cno='c003' --游标中的课程号如果为c003
then update SC set score = score + 5 where cno='c003' and sno=v_cno.sno;--则给c003课程加5分,如果没有cno='c003'这个条件,则会给整个表的成绩都加上5分。
else
update SC set score = score+10 where cno='c004' and sno=v_cno.sno;
end if;
end loop;
commit;
end;
下面这个方法与上面的思路一样,不过是把if换成了case罢了
create or replace procedure cj(v_sno SC.sno%type) is--创建存储过程
begin
for v_cno in(select * from SC where sno