扩大
缩小

使用游标——使用游标更新或删除数据


通过使用显式游标,不仅可以一行一行的处理select语句的结果,而且也可以更新或删除当前游标行的数据。注意,如果通过游标更新活是删除数据,那么在定义游标时必须要带有for update 子句。

例子:显示雇员名,工资,部门号,并给部门30的所有雇员增加200元工资:


declare 
cursor emp_cursor is select * from emp for update;
begin
for emp_record in emp_cursor loop
if emp_record.deptno=30 then
update emp set sal=sal+200 where current of emp_cursor;
dbms_output.put_line('姓名: '||emp_record.ename||' ,部门号 '||emp_record.deptno||
',原工资: '||emp_record.sal||',新工资: '||(emp_record.sal+200));
else
dbms_output.put_line('姓名: '||emp_record.ename||', 部门号:'||emp_record.deptno||
                     ',工资: '||emp_record.sal);
end if;
end loop;
end;
/

anonymous block completed
姓名: SMITH, 部门号:20,工资: 880
姓名: ALLEN ,部门号 30,原工资: 1600,新工资: 1800
姓名: WARD ,部门号 30,原工资: 1250,新工资: 1450
姓名: JONES, 部门号:20,工资: 3272.5
姓名: MARTIN ,部门号 30,原工资: 1250,新工资: 1450
姓名: BLAKE ,部门号 30,原工资: 2850,新工资: 3050
姓名: CLARK, 部门号:10,工资: 2450
姓名: SCOTT, 部门号:20,工资: 2000
姓名: KING, 部门号:10,工资: 5000
姓名: TURNER ,部门号 30,原工资: 1500,新工资: 1700
姓名: ADAMS, 部门号:20,工资: 1210
姓名: JAMES ,部门号 30,原工资: 950,新工资: 1150
姓名: FORD, 部门号:20,工资: 3300
姓名: MILLER, 部门号:10,工资: 2100


 

posted on 2013-05-01 10:40  LinuxPanda  阅读(4223)  评论(0编辑  收藏  举报

导航