Monday, November 16, 2009

Cursors In Oracle

declare
ans char(1);
begin
insert into newemp (empid,empname,job) values (109,'xyz','hr');
savepoint one_new_rec_ins;
update newemp set empname = 'abc' where empid = 109;
savepoint one_new_rec_upd;
delete from newemp where empid = 109;
ans := '&complete_rollback';
if ans = 'y' then
rollback to one_new_rec_ins;
elsif ans = 'n' then
rollback to one_new_rec_upd;
else
commit work;
end if;
end;

Parametrized Cursors

declare
cursor get_empl(salamt Number) is
select * from newemp where
salary > salamt and job <> 'President';
emprec newemp%rowtype;
begin
open get_empl(&amount);
dbms_output.put_line('records fetched are');
loop
fetch get_empl into emprec;
exit when get_empl%notfound;
dbms_output.put_line(emprec.empid||' '||emprec.empname||' '||emprec.job||' '||emprec.salary);
if emprec.job = 'manager'
then
update newemp set salary = salary + 500 where empid = emprec.empid;
elsif emprec.job = 'accountant' then
update newemp set salary = salary + 750 where empid = emprec.empid;
end if;
end loop;
close get_empl;
end;

No comments:

Blog Archive