oracle 存储过程 循环游标的简单实用示例

本文通过创建学生表并插入数据,演示了如何使用 Oracle PL/SQL 中的过程与游标来遍历记录,并展示如何读取表中的用户名与分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



基础数据准备:
create table STUDENT
(
  username CHAR(8),
  score    NUMBER
);
insert into student
  select 'zhangsan' username, 80 score from dual;
insert into student
  select 'lis' username, 90 score from dual;
insert into student
  select 'wangwu' username, 95 score from dual;
commit;

1.小示例:
create or replace procedure test as
  cursor cur is
    select username from student;
begin
  for Temp in cur loop
    dbms_output.put_line(Temp.username);
  end loop;
end test;

输出:
zhangsan
lis    
wangwu 


2.在原示例的基础上添加变量
create or replace procedure test1 is
  v_score number;
  v_username varchar(30);
  cursor cur is
    select username from student;
begin
  for Temp in cur loop
    select username into v_username from student where username=Temp.username;
    dbms_output.put_line(v_username);
    select score into v_score from student where username=Temp.username;
    dbms_output.put_line(v_score);
  end loop;
end test1;
输出:
zhangsan
80
lis    
90
wangwu 
95

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值