设为首页 收藏本站
查看: 895|回复: 0

[经验分享] Oracle 中的游标用法

[复制链接]

尚未签到

发表于 2016-7-19 11:03:11 | 显示全部楼层 |阅读模式
  
  循环将游标中的数据提取出来并放置2个变量中输出

SQL> declare
2    cursor dept_cur is select d.deptno,d.dname from scott.dept d;
3    dno scott.dept.deptno%type;
4    dnm scott.dept.dname%type;
5  begin
6    open dept_cur;
7    loop
8      fetch dept_cur into dno,dnm;
9      exit when dept_cur%notfound;
10      dbms_output.put_line(dno||' '||dnm);
11    end loop;
12    close dept_cur;
13  end;
14  /
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS



  循环将游标中的数据提取出来并放置 PL/SQL 记录中输出

declare
cursor dept_cur is select d.deptno,d.dname from scott.dept d;
type dept_record_type is record(
dno scott.dept.deptno%type,
dnm scott.dept.dname%type
);
dept_record dept_record_type;
begin
open dept_cur;
loop
fetch dept_cur into dept_record;
exit when dept_cur%notfound;
dbms_output.put_line(dept_record.dno||' '||dept_record.dnm);
end loop;
close dept_cur;
end;
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
  
  将游标中的所有数据一次性提取出来并放置 PL/SQL 记录表中循环输出

SQL> declare
2    cursor dept_cur is select d.deptno,d.dname from scott.dept d;
3    type dept_record_type is record(
4      dno scott.dept.deptno%type,
5      dnm scott.dept.dname%type
6    );
7    dept_record dept_record_type;
8  begin
9    open dept_cur;
10    loop
11      fetch dept_cur into dept_record;
12      exit when dept_cur%notfound;
13      dbms_output.put_line(dept_record.dno||' '||dept_record.dnm);
14    end loop;
15    close dept_cur;
16  end;
17  /
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
  
  每次从游标中提取指定行记录放置 PL/SQL 记录表中并循环输出

SQL> declare
2    cursor my_cur is select e.empno,e.ename from scott.emp e;
3    type emp_record_type is record(
4      eno scott.emp.empno%type,
5      enm scott.emp.ename%type
6    );
7    type emp_table_type is table of emp_record_type;
8    emp_table emp_table_type;
9    r int:=5;
10  begin
11    open my_cur;
12    loop
13      fetch my_cur bulk collect into emp_table limit r;    --批量处理不需要初始化变量大小
14      for i in 1..emp_table.count loop
15        dbms_output.put_line(emp_table(i).eno||' '||emp_table(i).enm);
16      end loop;
17      exit when my_cur%notfound;
18    end loop;
19    close my_cur;
20  end;
21  /
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
  
  将游标作为数据类型赋值后循环输出

SQL> declare
2    cursor my_cur is select e.empno,e.ename from scott.emp e;
3    type emp_table_type is table of my_cur%rowtype;
4    emp_table emp_table_type;
5    r int:=5;
6  begin
7    open my_cur;
8    loop
9      fetch my_cur bulk collect into emp_table limit r;
10      for i in 1..emp_table.count loop
11        dbms_output.put_line(emp_table(i).empno||' '||emp_table(i).ename);
12      end loop;
13      exit when my_cur%notfound;
14    end loop;
15    close my_cur;
16  end;
17  /
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER



  
  
  带参游标的使用
  


SQL> declare
2    cursor my_cur(eno int) is select e.empno,e.ename from scott.emp e where empno=eno;
3    emp_table my_cur%rowtype;
4  begin
5    open my_cur(7788);
6    fetch my_cur into emp_table;
7    dbms_output.put_line(emp_table.empno||' '||emp_table.ename);
8    close my_cur;
9  end;
10  /
7788 SCOTT
SQL> declare
2    cursor my_cur(eno int defalut 7788) is select e.empno,e.ename from scott.emp e where empno=eno;
3    emp_table my_cur%rowtype;
4  begin
5    open my_cur;
6    fetch my_cur into emp_table;
7    dbms_output.put_line(emp_table.empno||' '||emp_table.ename);
8    close my_cur;
9  end;
10  /
7788 SCOTT
   
  使用游标更新数据

declare
cursor my_cur is select e.sal from emp e for update;
v_oldsal emp.sal%type;
begin
open my_cur;
loop
fetch my_cur into v_oldsal;
exit when my_cur%notfound;
if v_oldsal < 2000 then
null;
update emp set sal=sal+100 where current of my_cur;
end if;
end loop;
close my_cur;
end;
  
  
  使用游标删除数据

declare
cursor my_cur is select deptno from emp2 for update;
dno dept.deptno%type;
begin
open my_cur;
loop
fetch my_cur into dno;
exit when my_cur%notfound;
if dno = 20 then
delete from emp2 where current of my_cur;
end if;
end loop;
close my_cur;
end;
  
  
  使用游标 FOR 循环

SQL> set serveroutput on
SQL> declare
2    cursor my_cur is select e.empno,e.ename from scott.emp e;
3    myemp my_cur%rowtype;
4  begin
5    for myemp in my_cur loop
6      dbms_output.put_line('第'||my_cur%rowcount||'个员工:'||myemp.ename);
7    end loop;
8  end;
9  /
第1个员工:SMITH
第2个员工:ALLEN
第3个员工:WARD
第4个员工:JONES
第5个员工:MARTIN
第6个员工:BLAKE
第7个员工:CLARK
第8个员工:SCOTT
第9个员工:KING
第10个员工:TURNER
第11个员工:ADAMS
第12个员工:JAMES
第13个员工:FORD
第14个员工:MILLER
PL/SQL procedure successfully completed
  
  在 FOR 循环中使用子查询

SQL> declare
2  begin
3    for emp_record in (select ename,sal from emp) loop
4      dbms_output.put_line(emp_record.ename);
5    end loop;
6  end;
7  /
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
PL/SQL procedure successfully completed
  
使用游标变量
  

declare
type ref_type_myc is ref cursor return emp%rowtype;
my_cur ref_type_myc;
type emp_table_type is table of emp%rowtype;
emp_table emp_table_type;
begin
open my_cur for select * from emp;
fetch my_cur bulk collect into emp_table;
for i in 1..emp_table.count loop
dbms_output.put_line(emp_table(i).ename);
end loop;
end;
  
嵌套游标的使用
  

SQL> set serverout on;
SQL> declare
2    type my_refcur_type is ref cursor;
3    my_refcur my_refcur_type;
4    cursor my_cur is
5      select d.dname,cursor(select ename,sal from emp where deptno=d.deptno) from dept d;
6    dnm dept.dname%type;
7    enm emp.ename%type;
8    sal emp.sal%type;
9  begin
10    open my_cur;
11    loop
12      fetch my_cur into dnm,my_refcur;
13      exit when my_cur%notfound;
14      dbms_output.put_line(dnm);
15      loop
16        fetch my_refcur into enm,sal;
17        exit when my_refcur%notfound;
18        dbms_output.put_line(enm||' '||sal);
19      end loop;
20    end loop;
21  end;
22  /
ACCOUNTING
CLARK 2695
KING 5500
MILLER 1530
RESEARCH
SMITH 900
JONES 2975
SCOTT 3000
ADAMS 1200
FORD 3000
SALES
ALLEN 1860
WARD 1475
MARTIN 1475
BLAKE 3135
TURNER 1750
JAMES 1145
OPERATIONS
PL/SQL procedure successfully completed

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-246261-1-1.html 上篇帖子: oracle数据表闪回技术 下篇帖子: Oracle SQL的练习(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表