CH6游标管理Word文档下载推荐.docx

上传人:b****1 文档编号:3670536 上传时间:2023-05-02 格式:DOCX 页数:9 大小:17.26KB
下载 相关 举报
CH6游标管理Word文档下载推荐.docx_第1页
第1页 / 共9页
CH6游标管理Word文档下载推荐.docx_第2页
第2页 / 共9页
CH6游标管理Word文档下载推荐.docx_第3页
第3页 / 共9页
CH6游标管理Word文档下载推荐.docx_第4页
第4页 / 共9页
CH6游标管理Word文档下载推荐.docx_第5页
第5页 / 共9页
CH6游标管理Word文档下载推荐.docx_第6页
第6页 / 共9页
CH6游标管理Word文档下载推荐.docx_第7页
第7页 / 共9页
CH6游标管理Word文档下载推荐.docx_第8页
第8页 / 共9页
CH6游标管理Word文档下载推荐.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

CH6游标管理Word文档下载推荐.docx

《CH6游标管理Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《CH6游标管理Word文档下载推荐.docx(9页珍藏版)》请在冰点文库上搜索。

CH6游标管理Word文档下载推荐.docx

没有找到记录....'

endif;

ifnotsql%isopenthen

dbms_output.put_line('

sql游标已经关闭'

endif;

commit;

end;

/*

显式游标用户自定义的游标

一般使用步骤:

声明游标:

cursor游标名is查询语句;

打开游标:

open游标名;

提取游标:

fetch游标名into变量;

关闭游标:

close游标名

*/

declare

cursorv_csisselect*fromscott.emp;

--声明游标

v_tempv_cs%rowtype;

openv_cs;

--打开游标

loop

fetchv_csintov_temp;

--提取游标

dbms_output.put_line(v_temp.ename||'

'

||v_temp.hiredate);

exitwhenv_cs%notfound;

--如果游标没有记录退出循环

endloop;

closev_cs;

--关闭游标

--游标属性说明

v_deptnumber;

cursorv_curisselect*fromscott.empwheredeptno=v_dept;

c_tempv_cur%rowtype;

v_dept:

=30;

openv_cur;

fetch前游标总记录数:

'

||v_cur%rowcount);

if(v_cur%isopen)then

游标已经打开'

loop

fetchv_curintoc_temp;

dbms_output.put('

游标总记录数:

||v_cur%rowcount||'

if(v_cur%found)then

dbms_output.put_line(c_temp.ename||'

||c_temp.sal);

游标已经没有记录'

exit;

endloop;

closev_cur;

/*带参数的游标

声明:

cursor游标名(形参数据类型...)isselect....where...=形参;

使用:

open游标名(实参);

v_inputscott.emp.deptno%type;

v_namescott.emp.ename%type;

v_salscott.emp.sal%type;

cursorc_cur(v_paramnumber)isselectename,salfromscott.empwheredeptno=v_param;

v_input:

='

&

部门编号'

;

openc_cur(v_input);

--传入实参

fetchc_curintov_name,v_sal;

exitwhenc_cur%notfound;

dbms_output.put_line(v_name||'

||v_sal);

closec_cur;

用于delete/update的游标

声明时指定forupdate[of列][nowait]加行级锁

[of列]如果涉及到多张表,默认会在所有表上加行共享锁,

为了只在特定表上加行共享锁,需要在forupdate子句后带有of子句指定字段名

更新时update......wherecurrentof游标名

declare

cursorc_curisselect*fromscott.empforupdate;

v_tempemp%rowtype;

openc_cur;

fetchc_curintov_temp;

exitwhenc_cur%notfound;

if(v_temp.sal<

1000)then

updatescott.empsetsal=sal+1wherecurrentofc_cur;

closec_cur;

 

for循环游标

自动打开,关闭,自动获取记录

cursorcursor_nameisselect......

forv_nameincursor_name(或select语句)

loop.....endloop;

例1

cursorc_curisselect*fromscott.emp;

begin

forv_tempinc_curloop--v_temp局部变量,无需声明

dbms_output.put_line(v_temp.ename||'

||v_temp.sal);

endloop;

例2

v_inputvarchar2

(1);

v_input:

请选择'

ifv_input='

a'

then

forv_recin(select*fromemp)

dbms_output.put_line(v_rec.empno||'

:

||v_rec.ename||'

||

v_rec.hiredate||'

||v_rec.sal);

elsifv_input='

b'

forv_recin(select*fromdept)

dbms_output.put_line(v_rec.deptno||'

||v_rec.dname);

pleaseinput...'

return;

/*********for循环游标(带参数)*************

声明游标:

cursorcursor_name(形参列表)isselect......

forv_nameincursor_name(实参)

****/

v_inputemp.deptno%type;

cursorc(deptnumber)isselect*fromempwheredeptno=dept;

forv_recinc(v_input)loop

dbms_output.put_line(v_rec.ename||'

/**************refcursor游标*******************

用于处理运行时动态执行的SQL查询即在open游标时动态指定sql语句

声明refcursor类型:

TYPE<

ref_cursor_name>

ISREFCURSOR[RETURN<

return_type>

];

声明ref游标类型的游标变量:

变量名游标名;

打开方式:

opencursor_namefor查询语句

***/

声明强类型的REF游标:

TYPEt_refcurISREFCURSORRETURNemp%ROWTYPE;

V_curt_refcur;

声明弱类型的REF游标(用于动态SQL)

TYPEt_refcurISREFCURSOR;

typev_refcurisrefcursor;

---声明refcursor类型

refcurv_refcur;

--声明refcursor类型的游标变量

v_idnumber;

v_namevarchar2(20);

v_selectionvarchar2

(1):

=upper(substr('

请输入员工(E)或部门编号(D)'

1,1));

begin

ifv_selection='

E'

openrefcurforselectempno,enamefromemp;

--打开游标

====员工信息===='

elsifv_selection='

D'

openrefcurforselectdeptno,dnamefromdept;

====部门信息===='

请输入员工信息(E)或部门信息(D)'

--返回

fetchrefcurintov_id,v_name;

--提取游标

whilerefcur%foundloop

#'

||v_id||'

||v_name);

closerefcur;

--关闭游标

/*******动态SQL(使用refcursor返回多行记录)***********

使用占位符

Opencursor_nameforv_sql[using占位符对应的变量]

*****/

v_curv_refcur;

v_empemp%rowtype;

v_salarynumber;

v_sqlvarchar2(100);

v_dnonumber;

v_salary:

请输入薪水标准'

v_dno:

请输入部门编号'

v_sql:

select*fromempwheresal>

1anddeptno=:

2orderbysaldesc'

openv_curforv_sqlusingv_salary,v_dno;

薪水大于'

||v_salary||'

的员工:

fetchv_curintov_emp;

exitwhenv_cur%notfound;

姓名:

||v_emp.ename||'

薪水:

||v_emp.sal);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2