【Oracle数据库】实验-游标cursor

1、游标的四个步骤:
【Oracle数据库】实验-游标cursor_第1张图片

declare
    cursor c_1 is select xm from xs;
    v_1 xs.xm%type;
begin
    open c_1;
    fetch c_1 into v_1;
    dbms_output.put_line(v_1);
end;

【Oracle数据库】实验-游标cursor_第2张图片
2、游标+%rowtype

declare
    cursor c_1 is select * from xs;
    v_1 xs%rowtype;
begin
    open c_1;
    fetch c_1 into v_1;
    dbms_output.put_line(v_1.xh||' '||v_1.xm);
    close c_1;
end;

【Oracle数据库】实验-游标cursor_第3张图片
3、带参数的游标

declare
    cursor c_1(v_xb xs.xb%type) 
        is select * from xs where xb=v_xb;
    v_1 xs%rowtype;
begin
    open c_1('男');
    fetch c_1 into v_1;
    dbms_output.put_line(v_1.xh||' '||v_1.xm);
    close c_1;
end;

【Oracle数据库】实验-游标cursor_第4张图片
4、游标的多表查询

declare
    cursor c_4 is select empno,ename,emp.deptno,dname
        from scott.emp,scott.dept
            where emp.deptno=dept.deptno;
    v_4 c_4%rowtype;
begin
    open c_4;
    fetch c_4 into v_4;
    dbms_output.put_line(v_4.empno||' '||v_4.ename||' '||v_4.deptno||' '||v_4.dname);
    close c_4;
end;

【Oracle数据库】实验-游标cursor_第5张图片
5、使用%rowcount显示当前行数

declare
    cursor c_1 is select xm from xs;
    v_1 xs.xm%type;
begin
    open c_1;
    fetch c_1 into v_1;
    dbms_output.put_line(v_1||' '||c_1%rowcount);
end;

【Oracle数据库】实验-游标cursor_第6张图片
【例9-15】根据输入的部门号查询某个部门的员工信息,部门号在程序运行时指定。

declare
    v_deptno scott.emp.deptno%type;
    cursor c_emp is select * from scott.emp where scott.emp.deptno=v_deptno;
    v_emp c_emp%rowtype;
begin
    v_deptno:=&x;
    open c_emp;
    loop
      fetch c_emp into v_emp;
      exit when c_emp%notfound;
      dbms_output.put_line(v_emp.empno||' '||v_emp.ename||' '||v_emp.deptno||' '||v_emp.sal);
    end loop;
    close c_emp;
end;

【Oracle数据库】实验-游标cursor_第7张图片
【9-16】利用简单循环统计并输出各个部门的平均工资

declare
    cursor c_dept is select deptno,avg(sal) avgsal from scott.emp group by deptno;
    v_dept c_dept%rowtype;
begin
    open c_dept;
    loop
      fetch c_dept into v_dept;
      exit when c_dept%notfound;
      dbms_output.put_line(v_dept.deptno||' '||v_dept.avgsal);
    end loop;
    close c_dept;
end;

【Oracle数据库】实验-游标cursor_第8张图片
【9-17】利用while循环统计并输出各个部门的平均工资

declare
    cursor c_dept is select deptno,avg(sal) avgsal from scott.emp group by deptno;
    v_dept c_dept%rowtype;
begin
    open c_dept;
    fetch c_dept into v_dept;
    while c_dept%found 
    loop
      dbms_output.put_line(v_dept.deptno||' '||v_dept.avgsal);
      fetch c_dept into v_dept;
    end loop;
    close c_dept;
end;

【Oracle数据库】实验-游标cursor_第9张图片
【9-18】利用for循环统计并输出各个部门的平均工资

declare
    cursor c_dept is select deptno,avg(sal) avgsal from scott.emp group by deptno;
begin
for v_dept in c_dept 
loop
      dbms_output.put_line(v_dept.deptno||' '||v_dept.avgsal);
    end loop;
end;

【Oracle数据库】实验-游标cursor_第10张图片

带for update的游标

修改scott.emp表员工的工资,如果员工的部门号为10,工资提高100;部门号为20,工资提高150;部门号为30,工资提高200;否则工资提高250。

declare
  cursor c_emp is select * from scott.emp for update;
  v_zl number;
  v_emp c_emp%rowtype;
begin
  for v_emp in c_emp loop
    case v_emp.deptno
      when 10 then v_zl:=100;
      when 10 then v_zl:=150;
      when 10 then v_zl:=200;
      else v_zl:=250;
    end case;
    update scott.emp set sal=sal+v_zl
      where current of c_emp;
  end loop;
end;

【Oracle数据库】实验-游标cursor_第11张图片
修改emp表的工资,工资不足1000的,调整为1500,工资高于1000的,调整为原来工资的1.5倍,调整后,若工资〉10000,则设其为10000。

declare
  cursor c_1 is select empno,sal from scott.emp for update of sal;
  v_sal scott.emp.sal%type;
begin
  for v_1 in c_1 loop
    if v_1.sal<=1000 then v_sal:=1500;
    else
      v_sal:=v_1.sal*1.5;
      if v_sal>10000 then v_sal:=10000;
      end if;
    end if; 
    update scott.emp set sal=v_sal
      where current of c_1;
  end loop;
end;

【Oracle数据库】实验-游标cursor_第12张图片

你可能感兴趣的:(Oracle数据库,数据库,oracle,database)