Oracle笔记 九、PL/SQL 游标的使用
2012-09-06 15:17
489 查看
--演示隐式游标,系统自动声明,自动打开,自动使用并且自动关闭
begin
updateempsetsal=1000;
dbms_output.put_line('影响的行数:'||sql%rowcount);
end;
rollback;
/*游标的使用方法:
第一步:声明游标
第二步:打开游标
第三步:使用游标进行循环操作
第四步:关闭游标*/
--普通游标,游标本身就是一个变量
declare
--下面的这行代码声明了一个游标
cursormycurisselect*fromempwheredeptno=20;
emprowemp%rowtype;
begin
openmycur;--打开游标
loop
fetchmycurintoemprow;--把游标所指的纪录放到变量中
exitwhen(mycur%notfound);--当游标没有指向行时退出循环
dbms_output.put_line('名字:'||emprow.ename||'薪水:'||emprow.sal);
endloop;
closemycur;--关闭游标
end;
--简单游标,列操作
declare
empnameemp.ename%type;
empsalemp.sal%type;
cursormycurisselectename,salfromempwheredeptno=30;
begin
openmycur;
loop
fetchmycurintoempname,empsal;
exitwhenmycur%notfound;
dbms_output.put_line('姓名:'||empname||'工资'||empsal);
endloop;
end;
--简单游标,列操作
declare
cursorc
is
select*fromdept;
vDept_row_recordc%rowtype;
begin
openc;
fetchcintovDept_row_record;
dbms_output.put_line(vDept_row_record.dname);
closec;
end;
--when循环游标
declare
cursorc
is
select*fromdept;
vDept_row_recordc%rowtype;
begin
openc;
loop
fetchcintovDept_row_record;
exitwhen(c%notfound);
dbms_output.put_line(vDept_row_record.dname);
endloop;
closec;
end;
--while循环游标
declare
cursorc
is
select*fromdept;
vDept_row_recordc%rowtype;
begin
openc;
fetchcintovDept_row_record;
while(c%found)loop
dbms_output.put_line(vDept_row_record.dname);
fetchcintovDept_row_record;
endloop;
closec;
end;
--for循环游标
declare
cursorc
is
select*fromdept;
vDept_row_recordc%rowtype;
begin
forvDept_row_recordincloop
dbms_output.put_line(vDept_row_record.dname);
endloop;
end;
--带参游标
declare
cursorc(sSalemp.sal%type,sEmpnoemp.empno%type)
is
select*fromempwheresal>=sSalandempno>sEmpno;
begin
forrecord_datainc(2500,6666)loop
dbms_output.put_line(record_data.ename);
endloop;
end;
--update游标
declare
cursorc(sSalemp2.sal%type)
is
select*fromemp2wheresal>=sSalforupdate;
begin
forrecord_datainc(2500)loop
if(record_data.sal<3000)then
updateemp2setsal=sal+3wherecurrentofc;
dbms_output.put_line(record_data.ename);
elsif(record_data.sal=5000)then
updateemp2setsal=sal-3wherecurrentofc;
dbms_output.put_line(record_data.ename);
endif;
endloop;
end;
--引用游标不能使用循环游标的语法
--引用游标不能进行删除和修改
--引用游标是一个数据类型,使用该类型必须声明变量
--弱类型引用游标,就是不指定游标将要提取的数据行的类型
declare
typemy_cur_typeisrefcursor;
mycurmy_cur_type;--声明变量
whichvarchar2(10);
deptrowdept%rowtype;
emprowemp%rowtype;
begin
which:='&请选择dept还是emp';
if(which='dept')then
openmycurforselect*fromdept;
loop
fetchmycurintodeptrow;
exitwhen(mycur%notfound);
dbms_output.put_line(deptrow.deptno||''||deptrow.dname);
endloop;
elsif(which='emp')then
openmycurforselect*fromemp;
loop
fetchmycurintoemprow;
exitwhen(mycur%notfound);
dbms_output.put_line(emprow.empno||''||emprow.ename);
endloop;
endif;
closemycur;
end;
--强类型引用游标,就是指定游标将要提取的数据行的类型,只能是record或%rowtype类型
--比如:returnnumber是错的,returnemp.ename%type也是错的
declare
typemycurtypeisrefcursorreturnemp%rowtype;
mycurmycurtype;--声明变量
emprowemp%rowtype;
begin
openmycurforselect*fromemp;
loop
fetchmycurintoemprow;
exitwhenmycur%notfound;
dbms_output.put_line(emprow.empno||''||emprow.ename);
endloop;
closemycur;
end;
相关文章推荐
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- Oracle笔记 九、PL/SQL 游标的使用
- ORACLE学习之PL/SQL编程——使用游标
- oracle pl/sql之使用游标变量更新数据库
- Oracle 笔记(八)、PL/SQL 高级应用(游标、存储过程、函数、程序包)
- oracle pl/sql 入门+ 数组使用+游标+动态SQL
- Oracle 笔记(八)、PL/SQL 高级应用(游标、存储过程、函数、程序包)
- PL/SQL ORACLE游标的使用方法
- [Oracle]在PL/SQL中更改数据和使用游标获取数据
- Oracle 笔记(八)、PL/SQL 高级应用(游标、存储过程、函数、程序包)
- Oracle 学习:PL/SQL循序渐进全面学习教程--课程十三 使用组合数据类型* 游标操纵数据
- Oracle 笔记(八)、PL/SQL 高级应用(游标、存储过程、函数、程序包)
- [oracle编程]pl/sql --标量 复合变量以及游标的简单使用