Oracle关于游标的使用全解

— 声明游标;cursor cursor_name is select_statement

–for 循环游标

–(1)定义游标

–(2)定义游标变量

–(3)使用for循环来使用这个游标

declare

–类型定义

cursor c_job

is

select empno,ename,job,sal

from emp

where job=’manager’;

–定义一个游标变量v_cinfo c_emp%rowtype ,该类型为游标c_emp中的一行数据类型

c_row c_job%rowtype;

begin

for c_row in c_job loop

dbms_output.put_line(c_row.empno||’-‘||c_row.ename||’-‘||c_row.job||’-‘||c_row.sal);

end loop;

end;

–fetch游标

–使用的时候必须要明确的打开和关闭

declare

–类型定义

cursor c_job

is

select empno,ename,job,sal

from emp

where job=’manager’;

–定义一个游标变量

c_row c_job%rowtype;

begin

open c_job;

loop

–提取一行数据到c_row

fetch c_job into c_row;

–判读是否提取到值,没取到值就退出

–取到值c_job%notfound 是false

–取不到值c_job%notfound 是true

exit when c_job%notfound;

dbms_output.put_line(c_row.empno||’-‘||c_row.ename||’-‘||c_row.job||’-‘||c_row.sal);

end loop;

–关闭游标

close c_job;

end;

–1:任意执行一个update操作,用隐式游标sql的属性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。

begin

update emp set ename=’aleark’ where empno=7469;

if sql%isopen then

dbms_output.put_line(‘openging’);

else

dbms_output.put_line(‘closing’);

end if;

if sql%found then

dbms_output.put_line(‘游标指向了有效行’);–判断游标是否指向有效行

else

dbms_output.put_line(‘sorry’);

end if;

if sql%notfound then

dbms_output.put_line(‘also sorry’);

else

dbms_output.put_line(‘haha’);

end if;

dbms_output.put_line(sql%rowcount);

exception

when no_data_found then

dbms_output.put_line(‘sorry no data’);

when too_many_rows then

dbms_output.put_line(‘too many rows’);

end;

declare

empnumber emp.empno%type;

empname emp.ename%type;

begin

if sql%isopen then

dbms_output.put_line(‘cursor is opinging’);

else

dbms_output.put_line(‘cursor is close’);

end if;

if sql%notfound then

dbms_output.put_line(‘no value’);

else

dbms_output.put_line(empnumber);

end if;

dbms_output.put_line(sql%rowcount);

dbms_output.put_line(‘————-‘);

select empno,ename into empnumber,empname from emp where empno=7499;

dbms_output.put_line(sql%rowcount);

if sql%isopen then

dbms_output.put_line(‘cursor is opinging’);

else

dbms_output.put_line(‘cursor is closing’);

end if;

if sql%notfound then

dbms_output.put_line(‘no value’);

else

dbms_output.put_line(empnumber);

end if;

exception

when no_data_found then

dbms_output.put_line(‘no value’);

when too_many_rows then

dbms_output.put_line(‘too many rows’);

end;

–2,使用游标和loop循环来显示所有部门的名称

–游标声明

declare

cursor csr_dept

is

–select语句

select dname

from depth;

–指定行指针,这句话应该是指定和csr_dept行类型相同的变量

row_dept csr_dept%rowtype;

begin

–for循环

for row_dept in csr_dept loop

dbms_output.put_line(‘部门名称:’||row_dept.dname);

end loop;

end;

–3,使用游标和while循环来显示所有部门的的地理位置(用%found属性)

declare

–游标声明

cursor csr_testwhile

is

–select语句

select loc

from depth;

–指定行指针

row_loc csr_testwhile%rowtype;

begin

–打开游标

open csr_testwhile;

–给第一行喂数据

fetch csr_testwhile into row_loc;

–测试是否有数据,并执行循环

while csr_testwhile%found loop

dbms_output.put_line(‘部门地点:’||row_loc.loc);

–给下一行喂数据

fetch csr_testwhile into row_loc;

end loop;

close csr_testwhile;

end;

select * from emp

–4,接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)

–cursor cursor_name[(parameter[,parameter],…)] is select_statement;

–定义参数的语法如下:parameter_name [in] data_type[{:=|default} value]

declare

cursor

c_dept(p_deptno number)

is

select * from emp where emp.depno=p_deptno;

r_emp emp%rowtype;

begin

for r_emp in c_dept(20) loop

dbms_output.put_line(‘员工号:’||r_emp.empno||’员工名:’||r_emp.ename||’工资:’||r_emp.sal);

end loop;

end;

select * from emp

–5:向游标传递一个工种,显示此工种的所有雇员的所有信息(使用参数游标)

declare

cursor

c_job(p_job nvarchar2)

is

select * from emp where job=p_job;

r_job emp%rowtype;

begin

for r_job in c_job(‘clerk’) loop

dbms_output.put_line(‘员工号’||r_job.empno||’ ‘||’员工姓名’||r_job.ename);

end loop;

end;

select * from emp

–6:用更新游标来为雇员加佣金:(用if实现,创建一个与emp表一摸一样的emp1表,对emp1表进行修改操作),并将更新前后的数据输出出来

–https://zheng12tian.iteye.com/blog/815770

create table emp1 as select * from emp;

declare

cursor

csr_update

is

select * from emp1 for update of sal;

empinfo csr_update%rowtype;

saleinfo emp1.sal%type;

begin

for empinfo in csr_update loop

if empinfo.sal<1500 then

saleinfo:=empinfo.sal*1.2;

elsif empinfo.sal<2000 then

saleinfo:=empinfo.sal*1.5;

elsif empinfo.sal<3000 then

saleinfo:=empinfo.sal*2;

end if;

update emp1 set sal=saleinfo where current of csr_update;

end loop;

end;

–7:编写一个pl/sql程序块,对名字以‘a’或‘s’开始的所有雇员按他们的基本薪水(sal)的10%给他们加薪(对emp1表进行修改操作)

declare

cursor

csr_addsal

is

select * from emp1 where ename like ‘a%’ or ename like ‘s%’ for update of sal;

r_addsal csr_addsal%rowtype;

saleinfo emp1.sal%type;

begin

for r_addsal in csr_addsal loop

dbms_output.put_line(r_addsal.ename||’原来的工资:’||r_addsal.sal);

saleinfo:=r_addsal.sal*1.1;

update emp1 set sal=saleinfo where current of csr_addsal;

end loop;

end;

–8:编写一个pl/sql程序块,对所有的salesman增加佣金(comm)500

declare

cursor

csr_addcomm(p_job nvarchar2)

is

select * from emp1 where job=p_job for update of comm;

r_addcomm emp1%rowtype;

comminfo emp1.comm%type;

begin

for r_addcomm in csr_addcomm(‘salesman’) loop

comminfo:=r_addcomm.comm+500;

update emp1 set comm=comminfo where current of csr_addcomm;

end loop;

end;

–9:编写一个pl/sql程序块,以提升2个资格最老的职员为manager(工作时间越长,资格越老)

–(提示:可以定义一个变量作为计数器控制游标只提取两条数据;也可以在声明游标的时候把雇员中资格最老的两个人查出来放到游标中。)

declare

cursor crs_testcomput

is

select * from emp1 order by hiredate asc;

–计数器

top_two number:=2;

r_testcomput crs_testcomput%rowtype;

begin

open crs_testcomput;

fetch crs_testcomput into r_testcomput;

while top_two>0 loop

dbms_output.put_line(‘员工姓名:’||r_testcomput.ename||’ 工作时间:’||r_testcomput.hiredate);

–计速器减一

top_two:=top_two-1;

fetch crs_testcomput into r_testcomput;

end loop;

close crs_testcomput;

end;

–10:编写一个pl/sql程序块,对所有雇员按他们的基本薪水(sal)的20%为他们加薪,

–如果增加的薪水大于300就取消加薪(对emp1表进行修改操作,并将更新前后的数据输出出来)

declare

cursor

crs_upadatesal

is

select * from emp1 for update of sal;

r_updatesal crs_upadatesal%rowtype;

saladd emp1.sal%type;

salinfo emp1.sal%type;

begin

for r_updatesal in crs_upadatesal loop

saladd:= r_updatesal.sal*0.2;

if saladd>300 then

salinfo:=r_updatesal.sal;

dbms_output.put_line(r_updatesal.ename||’: 加薪失败。’||’薪水维持在:’||r_updatesal.sal);

else

salinfo:=r_updatesal.sal+saladd;

dbms_output.put_line(r_updatesal.ename||’: 加薪成功.’||’薪水变为:’||salinfo);

end if;

update emp1 set sal=salinfo where current of crs_upadatesal;

end loop;

end;

–11:将每位员工工作了多少年零多少月零多少天输出出来

–近似

–ceil(n)函数:取大于等于数值n的最小整数

–floor(n)函数:取小于等于数值n的最大整数

–truc的用法 https://publish.it168.com/2005/1028/20051028034101.shtml

declare

cursor

crs_workday

is

select ename,hiredate, trunc(months_between(sysdate, hiredate) / 12) as spandyears,

trunc(mod(months_between(sysdate, hiredate), 12)) as months,

trunc(mod(mod(sysdate – hiredate, 365), 12)) as days

from emp1;

r_workday crs_workday%rowtype;

begin

for r_workday in crs_workday loop

dbms_output.put_line(r_workday.ename||’已经工作了’||r_workday.spandyears||’年,零’||r_workday.months||’月,零’||r_workday.days||’天’);

end loop;

end;

–12:输入部门编号,按照下列加薪比例执行(用case实现,创建一个emp1表,修改emp1表的数据),并将更新前后的数据输出出来

— deptno raise(%)

— 10 5%

— 20 10%

— 30 15%

— 40 20%

— 加薪比例以现有的sal为标准

–case expr when comparison_expr then return_expr

–[, when comparison_expr then return_expr]… [else else_expr] end

declare

cursor

crs_casetest

is

select * from emp1 for update of sal;

r_casetest crs_casetest%rowtype;

salinfo emp1.sal%type;

begin

for r_casetest in crs_casetest loop

case

when r_casetest.depno=10

then salinfo:=r_casetest.sal*1.05;

when r_casetest.depno=20

then salinfo:=r_casetest.sal*1.1;

when r_casetest.depno=30

then salinfo:=r_casetest.sal*1.15;

when r_casetest.depno=40

then salinfo:=r_casetest.sal*1.2;

end case;

update emp1 set sal=salinfo where current of crs_casetest;

end loop;

end;

–13:对每位员工的薪水进行判断,如果该员工薪水高于其所在部门的平均薪水,则将其薪水减50元,输出更新前后的薪水,员工姓名,所在部门编号。

–avg([distinct|all] expr) over (analytic_clause)

—作用:

–按照analytic_clause中的规则求分组平均值。

–分析函数语法:

–function_name(,…)

–over

–()

–partition子句

–按照表达式分区(就是分组),如果省略了分区子句,则全部的结果集被看作是一个单一的组

select * from emp1

declare

cursor

crs_testavg

is

select empno,ename,job,sal,depno,avg(sal) over (partition by depno ) as dep_avg

from emp1 for update of sal;

r_testavg crs_testavg%rowtype;

salinfo emp1.sal%type;

begin

for r_testavg in crs_testavg loop

if r_testavg.sal>r_testavg.dep_avg then

salinfo:=r_testavg.sal-50;

end if;

update emp1 set sal=salinfo where current of crs_testavg;

end loop;

end;

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐