天天看点

PL/SQL——编程——例外处理

1、系统预定义异常

no_data_found

case_not_found

cursor_already_open

invalid_number

--no_data_found异常

set

serveroutput on;

--&no表示从控制台输入参数

declare

  v_ename

varchar2(30);--定义字符串变量

  v_sal number(7,2);

begin

  select

ename,sal into v_ename,v_sal from emp where empno=&no;

dbms_output.put_line(‘雇员名:‘||v_ename||‘工资:‘||v_sal);

exception

when no_data_found then

dbms_output.put_line(‘没有找到该雇员号!‘);

end;

/

2、用户自定义异常

create or replace

procedure ex_test(spno number) is

--定义一个例外

myexp

exception;

  update emp set sal = sal +1000 where empno =

spno;

  if sql%notfound then --表示sql语句没有执行更新操作

raise myexp;--触发异常

  end if;

  exception

  when myexp

then

  dbms_output.put_line(‘没有更新任何用户‘);

继续阅读