oracle数据库的游标使用问题

来源:百度知道 编辑:UC知道 时间:2024/07/03 12:48:30
题目大概意思是:用游标实现更新员工工资. 条件是1.小于1000的加500
2.在1000和4000之间的加100 超过4000的不加
最后把员工的原来工资 和 现在的 工资都 打印出来

请高手看看我的代码 为什么 firstly 和 secondly 的直会是一样的
secondly 可是我更新完以后才赋值的啊??? 郁闷一天了

declare

cursor cursor1 is select * from emp;

firstly number;
secondly number;

begin

for i in cursor1

loop

firstly:=i.sal;
if i.sal<1000 then
update emp set sal=sal+500 where sal=i.sal;

elsif i.sal<=4000 and i.sal>=1000 then
update emp set sal=sal+100 where sal=i.sal;

end if;

secondly:=i.sal;
dbms_output.put_line('员工名:'||i.ename||'原工资:'||firstly||'目前工资:'||secondly);
end loop;
end;

因为你没有提交更新。
应该这样写:
declare

cursor cursor1 is select * from emp;

firstly number;
secondly number;

begin

for i in cursor1

loop

firstly:=i.sal;
if i.sal<1000 then
update emp set sal=sal+500 where sal=i.sal;
secondly:=i.sal+500;
elsif i.sal<=4000 and i.sal>=1000 then
update emp set sal=sal+100 where sal=i.sal;
secondly:=i.sal+100;

end if;

dbms_output.put_line('员工名:'||i.ename||'原工资:'||firstly||'目前工资:'||secondly);
end loop;
commit;
end;