Oracle 异常处理函数SQLCODE和SQLERRM

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28061489/article/details/79619332
/*
异常处理函数
  异常处理函数用于取得Oracle错误号和错误信息,其中函数SQLCODE用于取得错误号,SQLERRM用于取得错误信息
  另外,通过使用内置过程raise_application_error,可以在创建子程序(过程、函数、包)时自定义错误号和错误信息
*/
declare
  v_empno emp.empno%type:=&empno;
  v_ename emp.ename%type:='&ename';
  v_deptno emp.deptno%type:=&deptno;
begin
  insert into emp(empno,ename,deptno) values(v_empno,v_ename,v_deptno);
  if sql%found then
    dbms_output.put_line('数据插入成功!');
    commit;
  end if;
  exception
    when others then
      dbms_output.put_line('错误号:'||sqlcode);
      dbms_output.put_line('错误信息:'||sqlerrm);
end;


/* 
  RAISE_APPLICATION_ERROR该过程用于在PL/SQL子程序中自定义错误信息
  语法格式:raise_application_error(error_number,message)
      error_number:用于定义错误号(-20000-20999)
      message:用于指定错误信息,长度不能超过2048个字节
*/
-- 创建存储过程
create or replace procedure change_sal(eno number,salary number)
is 
begin
  update emp set sal = salary where empno=eno;
  if sql%notfound then
    raise_application_error(-20002,'该员工不存在!');
  else
    dbms_output.put_line('更新成功!');
    commit;
  end if;
end;

-- 执行存储过程
begin
  change_sal(8888,800);
end;

猜你喜欢

转载自blog.csdn.net/qq_28061489/article/details/79619332
今日推荐