数据库之游标

1、游标的运用:

select返回多行数据时。
当在PL/SQL块中执行查询语句(select)和数据操纵语句(DML)式,Oracle会为其在内存中分配上下文区(Context Area),即缓冲区,游标是指向上下文区的指针。对于数据操纵语句和单行select into语句,Oracle会为他们分配隐含游标。

2、显示游标:

PL/SQL包含隐含游标和显示游标两种游标类型,其中隐含游标用于处理select into和DML语句,显示游标则专门用于处理select语句返回的多行数据。

declare
   v_dept_id employees.department_id%type :=&请输入部门编号;
   v_row employees%rowtype;
begin
       select *into v_row from employees s where s.department_id=v_dept_id;
end;

在这里插入图片描述
使用显示游标即可解决问题:

declare
  v_dept_id employees.department_id%type := &请输入部门编号;
  cursor my_cursor is
    select * from employees s where s.department_id = v_dept_id;
  v_row employees%rowtype;
begin
  open my_cursor;
  loop
    fetch my_cursor
      into v_row;
    dbms_output.put_line(v_row.salary);
    exit when my_cursor%notfound;
  end loop;
  close my_cursor;
end;

在这里插入图片描述

3、隐式游标:

---隐式游标sql:
begin
  delete from lanqiao;
  if sql%notfound then
     dbms_output.put_line('有数据');
  end if;
end;

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值