oracle存储过程简单实例 变量赋值 游标遍历

应用场景:
有两张表,学生表和对应的各科成绩表。
学生表student
字段: id int name varchar(20)
数值: 1 A
             2 B
成绩表score
字段: id int     studentid int    subjectid int    score int

数值:

               1            1                        1                     80

               2            1                         2                    90
               3            1                         3                   100
               4            2                         1                    60
               5            2                         2                    70

用存储过程来通过名字获取对应学生的成绩最大值的科目名称。本例的逻辑比较简单,用一句sql就可以实现,这里只是演示存储过程的基本语法。


创建包package:
create or replace package max_type as
type max_cursor is ref cursor;
end;


创建存储过程procedure:
create or replace procedure getMaxScore 
(
    xname in varchar,
    x_cur out MAX_TYPE.max_cursor
)
as
max_score number;
cur_score number;
cursor cur_1 is select score from score where studentid=(select id from student where name=xname);
begin
  max_score := 0;
  for everyrow in cur_1 loop
    begin
      if (everyrow.score > max_score) then
        max_score := everyrow.score;
      end if;
    end;
  end loop;
  open x_cur for select max_score from student where rownum=1;
end getMaxScore;


调用存储过程:
SQL>var a refcursor;
SQL>call getMaxScore('A', :a);
SQL>print a


输出结果:
100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值