IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

Oracle(光标、例外)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

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;
/

文章永久链接:https://tech.souyunku.com/?p=22380


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(88) 打赏



未经允许不得转载:搜云库技术团队 » Oracle(光标、例外)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367