oracle游標總結

2021-05-23 21:18:41 字數 4750 閱讀 8052

1.利用游標顯示職工號、來廠日期、職位、部門號、部門名、部門所在地、工資所處的等級。

declare

outempno int;

outhiredate date;

outjob varchar(10);

outdeptno int;

outloc varchar(10);

outgrade int;

cursor list_emp_dept_salgrade is select empno,hiredate,job,deptno,loc,grade from emp natural join dept,salgrade where sal between losal and hisal;

begin

open list_emp_dept_salgrade;

loop

fetch list_emp_dept_salgrade into outempno,outhiredate,outjob,outdeptno,outloc,outgrade;

exit when list_emp_dept_salgrade%notfound;

dbms_output.put_line(outempno||'    '||outhiredate||'    '||outjob||'    '||outdeptno||'    '||outloc||'    '||outgrade);

end loop;

close list_emp_dept_salgrade;

commit;

end;

2、為所有的經理漲工資,每人提公升10%,如果工作超過5年的話,再加3000元。

declare

outempno int;

outename varchar(10);

outjob varchar(10);

outhiredate date;

outsal int;

cursor list_emp is select empno,ename,job,hiredate,sal from emp where job='manager';

begin

open list_emp;

loop

fetch list_emp into outempno,outename,outjob,outhiredate,outsal;

exit when list_emp%notfound;

update emp set sal=(1+0.1)*outsal where empno=outempno;

if months_between(sysdate,outhiredate)>=60 then

update emp set sal=sal+3000 where empno=outempno;   

end if;

end loop;

close list_emp;

commit;

end;

第二種方法:

declare

cursor cur_emp is select * from emp where job='manager' for update of sal;

semp emp%rowtype;

begin

open cur_emp;

loop

fetch cur_emp into semp;

exit when cur_emp%notfound;

update emp set sal=(1+0.1)*sal where current of cur_emp;

if months_between(sysdate,outhiredate)>=60 then

update emp set sal=sal+3000 where current of cur_emp;//釋放當前行的鎖

end if;

end loop;

close cur_emp;

commit;

end;

3、將emp表中工作年限最長的兩個人提公升為經理。

declare

outhiredate date;

outjob varchar(10);

outempno number;

cursor list_emp is select job,hiredate,empno from emp  order by hiredate asc;

begin

for i in 1..2 loop

fetch list_emp into outjob,outhiredate,outempno;

exit when list_emp%notfound;

update emp set job='manager' where empno=outempno;

end loop;

commit;

end;

第二種方法:

declare

cursor cur_emp is select * from emp where job not like 'manager'  order by hiredate asc for update of job;

semp emp%rowtype;

begin

open cur_emp;

loop

fetch cur_emp into semp;

exit when cur_emp%rowcount>=2;

update emp set job='manager' where current of cur_emp;

end loop;

close cur_emp;

commit;

end;

4、將emp表中姓名以a或s開始的員工工資提公升10%。

declare

cursor cur_emp is select empno from emp where ename like 'a%' or ename like 's%';

outempno int;

begin

open cur_emp;

loop

fetch cur_emp into outempno;

exit when cur_emp%notfound;

update emp set sal=sal*1.1 where empno=outempno;

end loop;

close cur_emp;

commit;

end;

第二種方法:

declare

cursor cur_emp is select * from emp where ename like 'a%' or ename like 's%' for update of sal;

semp emp%rowtype;

begin

open cur_emp;

loop

fetch cur_emp into semp;

exit when cur_emp%notfound;

update emp set sal=sal*1.1 where current of cur_emp;

end loop;

close cur_emp;

commit;

end;

5.將員工的工資進行修改,少於1000 改為1000,在1001-2000 之間改為2500,2001-3000 之間改為3500。

declare

outempno int;

outsal int;

cursor list_emp is select empno,sal from emp;

begin

open list_emp;

loop

fetch list_emp into outempno,outsal;

exit when list_emp%notfound;

if outsal<1500 then

outsal:=1500;

elsif outsal<3000 then

outsal:=3000;

else

outsal:=5000;

end if;

update emp set sal=outsal where empno=outempno;

end loop;

dbms_output.put_line('更新成功');

close list_emp;

commit;

end;

declare

cursor cur_emp is select * from emp for update of sal;

semp emp%rowtype;

begin

open cur_emp;

loop

fetch cur_emp into semp;

exit when cur_emp%notfound;

if semp.sal<1500 then

semp.sal:=1500;

elsif semp.sal<3000 then

semp.sal:=3000;

else

semp.sal:=5000;

end if;

update emp set sal=semp.sal where current of sal;

end loop;

close cur_emp;

commit;

end;

oracle 游標總結

declare cursor c emp is select empno,ename,job,sal from emp where job manager v row c emp rowtype begin for v row in c emp loop dbms output.put line v...

Oracle 游標的使用總結

declare 定義游標 cursor tcount is select to number to char d create,yyyy as fyear,vc code as fsetid,l fundid as fsetcode,vc name as fsetname,nvl to number...

Oracle游標使用的經驗總結

以下的文章主要是介紹oracle 游標使用,以下就是具體方案的描述,希望在你今後的學習中會有所幫助。首先我們要用到的是select 語句,其用於從資料庫中查詢資料,當在pl sql 中使用select 語句時,要與into 子句一起使用,查詢的返回值被賦予into 子句中的變數,變數的宣告是在del...