游标 Oracle

游标

PL/SQL 的游标是指把从数据库中查询出来的数据以临时表的形式存放内中,游标可以对存放在内      存中的数据进行操作,返回一条或一组记录,或者一条也不返回
  pl/sql 中的记录和表类型虽然可以存储数据

游标的基本操作:
pl/sql 包含隐士游标和显示游标
1. 定义游标
    cursor cursor_name is select....
2. 打开游标
open cursor_name;
3. 提取游标
fetch cursor_name into variable1,variable2.....
4. 关闭游标
    close cursor_name

    cursor cursor_name is select...
        cursor : 游标关键字
        cursor_name : 游标名称
        select.. : 建立游标所使用的查询语句

declare
--1.定义游标
/*
cursor c_dept is select * from dept;
/
cursor c_dept is select deptno,dname from dept;
v_dname dept.dname% type;
v_dno dept.deptno%type;
v_dept dept%rowtype;
begin
--2. 打开游标
open c_dept;
--3. 提取游标
loop
/

fetch c_dept into v_dept;
dbms_output.put_line(v_dept.dname||' '||v_dept.loc);
*/
fetch c_dept into v_dno,v_dname;
dbms_output.put_line(v_dname||' '|| v_dno);
exit when c_dept%notfound;
end loop;
--4. 关闭游标
close c_dept;
end;

游标属性

游标作为一个临时表,可以通过游标的属性获取游标的状态
    1. %isopen 属性主要用于判断游标是否打开,在使用游标的时候如果不能确定是否已打开可以判断使用(为打开游标不可提取)
    2. %found  属性主要用于判断游标是否找到记录,如果找到记录用fetch语句提取游标数据
3. %notfound 如果提取到数据返回false否则返回true和%found正好相反
    4. %rowcount  该属性用于返回到当前提取的实际行数

declare 
   cursor emp_cursor(dno number) is select ename from emp where deptno = dno;
   v_name emp.ename%type;

begin
open emp_cursor(20);
loop
fetch emp_cursor into v_name;
dbms_output.put_line(v_name);
exit when emp_cursor%notfound;
end loop ;
close emp_cursor;

dbms_output.put_line('-------------');
open emp_cursor(10);
loop
fetch emp_cursor into v_name;
dbms_output.put_line(v_name);
exit when emp_cursor%notfound;
end loop ;
close emp_cursor;
end;

参数化游标

定义游标后,使用后再传参
cursor cursor_name(paramter) is select....

-- Created on 2019-09-04 by LINNE 
declare 
  cursor emp_cursor(dno number) is select ename from emp where deptno = dno;

  v_ename emp.ename%type;

begin
  -- Test statements here
  open emp_cursor(20);
  loop
    fetch emp_cursor into v_ename;
    dbms_output.put_line(v_ename);
    exit when emp_cursor%notfound;
  end loop;
  close emp_cursor;

   dbms_output.put_line('---------------------------------');

  open emp_cursor(10);
  loop
    fetch emp_cursor into v_ename;
    dbms_output.put_line(v_ename);
    exit when emp_cursor%notfound;
  end loop;
  close emp_cursor;
end;

游标for循环

游标for循环是在pl/sql 块中使用游标最简单的方式,简化了游标的处理,Oracle会隐含的打开游标,提取游标,关闭游标
for record in cursor_name loop
        .....
    end loop;
declare

 cursor c_dept(dno number) is select * from dept where deptno = dno;

begin

 for v_dept in c_dept(20) loop
   dbms_output.put_line('第'||c_dept%rowcount||'个员工'||v_dept.dname);
 end loop;
end;

隐式游标

在执行一个SQL语句的时候,Oracle服务器将自动创建一个隐式游标.隐式游标的固定名称 SQL,隐式游标不需要声明和打开.使用完也不需要关闭.
隐式游标只能处理一行数据,所以into只能给一组变量赋值!
操作游标(游标的删除和修改)
存储过程
函数
包
触发器
序列##  ##
同义词
视图
索引
备份和恢复
审计/数据库的数据加密/数据加载/数据传输/闪回..../日志恢复..

除了可以一行一行展示select结构外,还可以更新或删除,当前游标行数据,如果需  要对游标中的行数据进行修改或删除,在定义游标的时候必须携带for update 字句
cursor cursor_name is select .....for update

declare
cursor emp_cursor is select ename, sal ,deptno from emp for update;
v_ename emp.ename%type;
v_sal emp.sal%type;
v_deptno emp.deptno%type;
begin
  open emp_cursor;
loop 
  fetch emp_cursor into v_ename,v_sal,v_deptno;
  exit when emp_cursor%notfound;
 -- dbms_output.put_line(deptno||v_ename||v_sal); 
  update emp set deptno = deptno+ 10 where current of emp_cursor;        
end loop;
close emp_cursor;
end;
--select * from emp

declare 
  type emp_type is ref cursor return emp%rowtype;
  v_emp emp_type;
  rec_emp emp%rowtype;

begin
open v_emp for select * from emp;
loop
fetch v_emp into rec_emp;  
exit when v_emp%notfound;
dbms_output.put_line(rec_emp.ename);

end loop;
close v_emp;
dbms_output.put_line('-----------------------');

open v_emp for select * from emp order by deptno;
loop
  fetch v_emp into rec_emp;
exit when v_emp%notfound;
dbms_output.put_line(rec_emp.ename);
end loop;
close v_emp;
end;

</article>

0人点赞

随笔

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 4,827评论 0 0
  • pl/sql : PL/SQL 是Oracle在标准法人SQL语句上的过程性扩展。PL/SQL 不仅允许嵌入SQL...
    依恋_d554阅读 1,924评论 0 0
  • 在 PL/SQL 程序中,对于处理多行记录的事务经常使用游标来实现。 游标概念 为了处理 SQL 语句,ORACL...
    辽A丶孙悟空阅读 3,638评论 0 15
  • 人生有喜有悲,总有不愉快的事情发生 那有真的这么巧妙,老爸年过八十五岁,本 身体健康,走路平稳,有点高压病,活着当...
    殷照红阅读 4,430评论 18 38
  • 非常荣幸能有机会到二实验参加国培,听着老师们一句句感人肺腑的经历,艰难抉择之后成为师者,为人师后不言辛苦,勇于创新...
    zzzzzz赵贝贝阅读 803评论 0 0