我們有時采用select會返回一個結果集,使用簡單的select無法得到上一行,下一行,后5行,后10行,如果想做到這一點必須使用游標,游標是存儲在數據庫服務器上的一個數據庫查詢,它不是一條select語句,他是一個結果集,有了游標就可以根據需要滾動瀏覽數據了。
下面通過一個示例,根據崗位加工資,如果是MANAGER增加20%的工資,如果是SALESMAN增加10%的工資,其他的增加5%的工資。
把For update語句對數據的鎖定叫行級鎖。
create or replace procedure proc_sal
is
cursor c is
select * from emp for update;
begin
for v_emp in c loop
if (v_emp.job = 'MANAGER') then
update emp set sal = sal + sal*0.2 where current of c;
elsif (v_emp.job = 'SALESMAN') then
update emp set sal = sal + sal*0.1 where current of c;
else
update emp set sal = sal + sal*0.05 where current of c;
end if;
end loop;
commit;
end;
Select 語句的for update操作會對查詢結果集的數據進行加鎖。
執行存儲過程
exec proc_sal;