我們有時采用select會返回一個結(jié)果集,使用簡單的select無法得到上一行,下一行,后5行,后10行,如果想做到這一點(diǎn)必須使用游標(biāo),游標(biāo)是存儲在數(shù)據(jù)庫服務(wù)器上的一個數(shù)據(jù)庫查詢,它不是一條select語句,他是一個結(jié)果集,有了游標(biāo)就可以根據(jù)需要滾動瀏覽數(shù)據(jù)了。
下面通過一個示例,根據(jù)崗位加工資,如果是MANAGER增加20%的工資,如果是SALESMAN增加10%的工資,其他的增加5%的工資。
把For update語句對數(shù)據(jù)的鎖定叫行級鎖。
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操作會對查詢結(jié)果集的數(shù)據(jù)進(jìn)行加鎖。
執(zhí)行存儲過程
exec proc_sal;