zoukankan      html  css  js  c++  java
  • Oracle(光标、例外)

    1、光标

    (1)作用:

    当返回多条记录的时候我们就需要用到光标。

    (2)属性:

    光标是否打开:

    %isopen

    影响的行数:

    %rowcount

    找到:

    %found

    未找到:

    %notfound

    (3)查询员工的姓名和薪水:

    declare
    cursor cemp is select ename,sal from emp;--定义光标
    pename emp.ename%type;
    psal emp.sal%type;
    begin
    open cemp;--打开游标
    loop
    fetch cemp into pename,psal;--取一条记录
    exit when cemp%notfound;--为空
    SYS.DBMS_OUTPUT.PUT_LINE('员工'||pename||'的薪水为'||psal);
    end loop;
    close cemp;
    end;
    匿名块已完成
    员工SMITH的薪水为800
    员工ALLEN的薪水为1600
    员工WARD的薪水为1250
    员工JONES的薪水为2975
    员工MARTIN的薪水为1250
    员工BLAKE的薪水为2850
    员工CLARK的薪水为2450
    员工SCOTT的薪水为3000
    员工KING的薪水为5000
    员工TURNER的薪水为1500
    员工ADAMS的薪水为1100
    员工JAMES的薪水为950
    员工FORD的薪水为3000
    员工MILLER的薪水为1300

    (4)给员工加工资:

    rem PL/SQL Developer Test Script
    set feedback off
    set autoprint off
    rem Execute PL/SQL Block
    declare 
      cursor cemp is select empno,job from emp; --定义光标
      pempno emp.empno%type;
      pjob   emp.job%type;
    begin
      open cemp;  --打开光标
      loop
           fetch cemp into pempno,pjob;--取一个员工
           exit when cemp%notfound;
           
           if pjob = 'PRESIDENT' then update emp set sal=sal+1000 where empno=pempno; --判断职位
              elsif pjob = 'MANAGER' then update emp set sal=sal+800 where empno=pempno;
              else update emp set sal=sal+400 where empno=pempno;
           end if;
    
      end loop;
      close cemp;  --关闭光标
      commit;--提交
    end;
    /

    (5)带有参数的光标:

    查看部门的员工姓名:

    declare 
       cursor cemp(dno number) is select ename from emp where deptno=dno;
       pename emp.ename%type;
    begin
       open cemp(20);
       loop
            fetch cemp into pename;
            exit when cemp%notfound;
            dbms_output.put_line(pename);
       end loop;
       close cemp;
    end;
    /

    2、例外

    (1)系统定义的:

    no_data_found(没有找到数据)
    too_many_rows(select....into语句匹配多个行)
    zero_divide(被零除)
    value_error(算术或转换错误)
    timeout_on_resource(在等待资源时发生超时,分布式数据库)
    declare
       pnum number;
    begin
      pnum := 1/0;
    exception
      when zero_divide then dbms_output.put_line('1:0不能做分母');
      when value_error then dbms_output.put_line('算术或者转换错误');                      
      when others then dbms_output.put_line('其他例外');
    end;
    /

    注意:在Oracle中要处理掉所有的例外,如果将例外交给数据库将出错。

    (2)自定义例外:

    declare 
      cursor cemp  is select ename from emp where deptno=50;
      pename emp.ename%type;
      no_emp_found exception;
    begin
      open cemp;
      fetch cemp into pename;
      if cemp%notfound then
        raise no_emp_found;--抛出例外
      end if;
      close cemp;
    exception
      when no_emp_found then dbms_output.put_line('没有找到员工');
      when others then dbms_output.put_line('其他例外');
    end;
    /
  • 相关阅读:
    Linux之文件处理命令
    Linux基础命令
    rip实验
    Linux基础之磁盘分区
    mysql安装
    centos Apache、php、mysql默认安装路径
    You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.
    Wrong permissions on configuration file, should not be world writable!
    机器会学习么 学习总结
    实验 5 Spark SQL 编程初级实践
  • 原文地址:https://www.cnblogs.com/zhai1997/p/12383736.html
Copyright © 2011-2022 走看看