技术开发 频道

Oracle概念:异常和游标

  PRAGMA EXCEPTION_INIT

  Declare

  L_update_text varchar2(100):=

  ‘update &table_name set &updated_column_name= ‘’:a’’

  where &key_column_name=:a’;

  begin

  execute immediate L_update_text using ‘&updated_column_value’, &key_column_value;

  end;

  /

  Declare

  Invalid_column_name exception;

  Pragma exception_init(Invalid_column_name,-904);

  L_update_text varchar2(100):=

  ‘update &table_name set &updated_column_name= ‘’:a’’

  where &key_column_name=:a’;

  begin

  execute immediate L_update_text using ‘&updated_column_value’, &key_column_value;

  exception

  when Invalid_column_name then

  dbms_output.put_line(‘hehe’);

  end;

  /

  异常传播:

  begin

  begin

  begin

  begin

  begin

  declare

  fname employees.first_name%type;

  begin

  select first_name into fname from employees where 1=2;

  --exception

  --when NO_DATA_FOUND then

  -- dbms_output.put_line(‘block 6’);

  end;

  exception

  when NO_DATA_FOUND then

  dbms_output.put_line(‘block 5’);

  end;

  exception

  when NO_DATA_FOUND then

  dbms_output.put_line(‘block 4’);

  end;

  exception

  when NO_DATA_FOUND then

  dbms_output.put_line(‘block 3’);

  end;

  exception

  when NO_DATA_FOUND then

  dbms_output.put_line(‘block 2’);

  end;

  exception

  when NO_DATA_FOUND then

  dbms_output.put_line(‘block 1’);

  end;

  /

  作用域和可视性:

  begin

  declare

  nested_excp exception;

  begin

  raise nested_excp;

  end;

  exception

  when nested_excp then

  dbms_output.put_line('haha');

  end;

  /

  when others then

  放在最后

  begin

  declare

  nested_excp exception;

  begin

  raise nested_excp;

  end;

  exception

  when others then

  dbms_output.put_line('haha');

  dbms_output.put_line(sqlcode || ‘ is ’ || sqlerrm);

  raise;

  end;

  /

  SQLCODE SQLERRM

  declare

  l_dept departments%rowtype;

  begin

  l_dept.department_id:=100;

  l_dept.department_name:=’HR’;

  insert into departments(department_id, department_name)

  values(l_dept.department_id, l_dept.department_name);

  Exception

  When others then

  dbms_output.put_line(sqlcode || ‘ is ’ || sqlerrm);

  end;

  /

  declare

  l_dept departments%rowtype;

  begin

  l_dept.department_id:=100;

  l_dept.department_name:=’HR’;

  insert into departments(department_id, department_name)

  values(l_dept.department_id, l_dept.department_name);

  Exception

  When others then

  Raise_application_error(-20001, ‘error message!’);

  End;

  /

0
相关文章