`
weigang.gao
  • 浏览: 482378 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

exit when cursor%notfound 放在fetch的前面会比放在fetch后面多loop一个次

 
阅读更多

when the named cursor is opened, the pointer will be positioned to the first row of the result set, if exists. The cursor%notfound will return NULL, which has no affect to statement "exit when cursor%notfound" for the loop will not exit until cursor%notfound is evaluted as TRUE. The FETCH statemetn will retrieve the current row and move the pointer to the next row.  The cursor%notfound will return FALSE if the most recent fetch from the named cursor returned a row. So the best position for statement "exit when cursor%notfound" is immediated follow a FETCH cursor.  If  "exit when cursor%notfound" is put before FETCH, the last loop will process the row after the last row of the result set, which is NULL. PL/SQL will not raise an exception when fetch statement returns no rows.

 测试:

a.建表

create table tb_student(
student_id number,--学号
student_name varchar2(30),
student_sex char(1),--性别 F/M
student_age number,--年龄
student_birthday date,--学生出生日期
student_address varchar2(50),--住址
student_desc varchar2(200),--备注
constraint pk_student primary key(student_id)
);

 

 b.插入数据

insert into tb_student(student_id, student_name, student_sex, student_age, student_birthday) 
  values (seq_student.nextval, '高红成', 'F', '26', to_date('1971-02-12', 'yyyy-MM-dd') );
insert into tb_student(student_id, student_name, student_sex, student_age, student_birthday) 
 values (seq_student.nextval, '李艳', 'F','25', to_date('1981-05-09', 'yyyy-MM-dd') );
insert into tb_student(student_id, student_name, student_sex, student_age, student_birthday) 
 values (seq_student.nextval, '高雄', 'M','21', to_date('1981-05-09', 'yyyy-MM-dd') );
insert into tb_student(student_id, student_name, student_sex, student_age, student_birthday) 
 values (seq_student.nextval, '彭传志', 'M','22', to_date('1981-05-09', 'yyyy-MM-dd') );

 c.语句块

--判断是不是null,要用is 而不能是"="
--判断是不是true,用"=",而不是is 
declare
cursor cur_name is select student_name from tb_student t;
tempName tb_student.student_name%type;
begin
  open cur_name;
     loop
         if cur_name%notfound is null then --第一执行到这里的时候 cur_name%notfound为null
             dbms_output.put_line('null');
         end if;
         if cur_name%notfound = true then
             dbms_output.put_line('true');
         end if;
        if cur_name%notfound = false then
             dbms_output.put_line('false');
        end if;
     -- 1.当游标指针移动到最后一条记录时,就不会再移动了。
     -- 2.因为%notfound是根据最近的一次fetch来判断的,因此当输出最后一条记录后,还会在输出最一条记录,即最后一条记录将输出2遍。
     -- 3.If  "exit when cursor%notfound" is put before FETCH, the last loop will process the row after the last row of the result set, which is NULL. 
     --   PL/SQL will not raise an exception when fetch statement returns no rows.
     --   最后一次循环将处理在"结果集最后一行"的下一行,该行为NULL,当fetch的时候PL/SQL将不会抛出异常,并且不会覆盖最后一次循环中赋予tempName的值,即tempName会保留最后一次被赋予的值
     
     exit when cur_name%notfound;--当cur_name%notfound为null的时候,并不会退出。直到为true才会退出
        fetch cur_name into tempName; --当游标指针移动到 最后一条 记录后,就不会再移动了,还是指向最后一条记录,因此最后一条记录才会输出2遍
        if cur_name%notfound is null then
           dbms_output.put_line('null...');
        end if;
        if cur_name%notfound = true then
           dbms_output.put_line('true...');
        end if;
        if cur_name%notfound = false then
           dbms_output.put_line('false...');
        end if;
        dbms_output.put_line(tempName);
   end loop;
  close cur_name;
end;

 

2.为什么"exit when cur_name%notfound;" 需要与fetch 配合使用呢?

因为如果不使用fetch语句的话 "cur_name%notfound"将始终为null,将进入死循环

declare
cursor cur_name is select student_name from tb_student t;
tempName tb_student.student_name%type;
begin
open cur_name;
 loop
  exit when cur_name%notfound;
  tempName := 'gaoweigang';
  dbms_output.put_line(tempName);
  end loop;
close cur_name;--修改后
end;

 

 

分享到:
评论

相关推荐

    ORACLE NO_DATA_FOUND的三种处理办法

    EXIT WHEN cursor_name%NOTFOUND; -- 当前查询行的处理逻辑 END LOOP; CLOSE cursor_name; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('没有找到匹配的数据'); -- 其他可能的异常处理 END; / `...

    oracle-cursor.rar_cursor_oracle_oracle cursor

    EXIT WHEN cursor_name%NOTFOUND; -- 更新操作 UPDATE table_name SET value = new_value WHERE id = v_id; END LOOP; CLOSE cursor_name; END; / ``` 在这个例子中,游标被用来逐行处理满足条件的记录,对每行...

    oracle中游标的使用

    Exit when student_cursor%notfound; (if student_cursor%rowcount=8 then exit;End if) End loop; Close student_cursor; End; ``` ### 示例 2:使用游标修改数据 使用游标可以修改数据表中的记录。例如,调整...

    Oracle 返回多个结果集

    EXIT WHEN cursor1%NOTFOUND; -- 打印或处理结果 END LOOP; CLOSE cursor1; -- 处理第二个结果集 OPEN cursor2; LOOP FETCH cursor2 INTO var3, var4; EXIT WHEN cursor2%NOTFOUND; -- 打印或处理结果 ...

    Oracle中Cursor介绍[归类].pdf

    exit when cur_policy%NOTFOUND; -- process the data here end loop; close cur_policy; end; ``` 在这个例子中,我们定义了一个显式游标cur_policy,用于查询特定条件的合同信息,然后在循环中通过FETCH操作...

    修改后的oracle游标总结,比较的全面

    Exit when V_STUDENT%notfound; -- 这里可以做一些其它的操作,比如判断、修改、删除等等 Dbms_output.putline('v_name(名称):'||v_name||'v_score(分数):'||v_score); End loop; Close V_STUDENT; End ...

    oracle CURSOR例子

    EXIT WHEN mycur%NOTFOUND OR mycur%ROWCOUNT IS NULL; DBMS_OUTPUT.PUT_LINE('当前游标行号: ' || mycur%ROWCOUNT); -- 获取当前行号 END LOOP; CLOSE mycur; END; ``` 本示例中,我们展示了如何使用`%ROWCOUNT...

    oracle的游标使用

    EXIT WHEN emp_cursor%NOTFOUND; ``` 6. **关闭游标**:释放与游标相关的资源。 ```sql CLOSE emp_cursor; ``` #### 四、示例分析 以下是一个使用显式游标查询20号部门所有员工的信息的例子: ```sql DECLARE...

    Oracle PLSQL游标的学习

    exit when mycur%notfound; dbms_output.put_line('雇员编号:' || person1.empno || ',地址:' || person1.empzc); end loop; close mycur; end; ``` 六、游标 for 循环 游标 for 循环是一种快捷使用游标的方式...

    Oracle_的存储过程及游标

    - 在循环处理游标中的数据时,通常使用`EXIT WHEN cursor%NOTFOUND`来结束循环,避免空指针异常。 通过理解并熟练运用Oracle的存储过程和游标,开发者可以构建高效、模块化的数据库应用,提高代码复用性和数据库...

    oracle游标使用大全.doc

    `%NOTFOUND`属性在`FETCH`之后检查是否还有更多行可供读取,如果没有,它将返回`TRUE`,从而退出循环。 此外,Oracle还支持使用记录变量来简化处理过程。记录变量通过`%ROWTYPE`关键字定义,它能直接映射到表的一行...

    Oracle游标使用

    EXIT WHEN cursor_name%NOTFOUND; -- 处理数据 END LOOP; CLOSE cursor_name; END; ``` **隐式游标** 是Oracle自动管理的,当执行DML(INSERT、UPDATE、DELETE)操作或单行SELECT...INTO...查询时,系统会自动...

    先查询后更新的oralce函数

    EXIT WHEN fCursor%NOTFOUND; END LOOP; CLOSE fCursor; RETURN 0; END getTimes; ``` **解析:** 1. **函数声明**:定义了一个名为`getTimes`的函数,返回类型为`INTEGER`。 2. **变量声明**:声明了四个变量`...

    oracle游标的使用

    EXIT WHEN cursor_name%NOTFOUND; -- 处理每一行数据的代码 END LOOP; CLOSE cursor_name; END; ``` 在游标声明中,`cursor_name`是你自定义的游标名称,`FOR`后面的`SELECT`语句定义了游标要获取的数据。`...

    Oracle数据库中的Cursor介绍

    EXIT WHEN myCur%NOTFOUND; -- 处理每一条记录 END LOOP; CLOSE myCur; END; ``` 综上所述,游标是Oracle数据库中一项非常重要的特性,它使得应用程序能够更加高效地处理查询结果。然而,合理选择和使用游标...

    ORACLE_cusor.rar_oracle

    EXIT WHEN cursor_name%NOTFOUND; -- 处理每一行数据 dbms_output.put_line(var_column1 || ', ' || var_column2); END LOOP; CLOSE cursor_name; END; / ``` 2. **隐式游标**: Oracle数据库在每个PL/SQL...

    plsql.zip_PLSQL Developer_cursor

    EXIT WHEN my_cursor%NOTFOUND; -- 处理emp_rec记录 END LOOP; CLOSE my_cursor; END; ``` 在这个例子中,我们声明了一个名为my_cursor的显示游标,用于获取department_id为100的员工信息。然后,我们使用`OPEN`...

    oracle游标介绍[定义].pdf

    exit when myCur%NOTFOUND; dbms_output.put_line(vno||' '||vna||' '||vsal); end loop; close myCur; End; / ``` 尽管游标在某些情况下是必要的,但应谨慎使用,因为它们可能导致性能问题,特别是在处理大量...

    oracle游标使用及实例

    - 在使用`FETCH INTO`时,可以使用`EXIT WHEN cur_name%NOTFOUND`来判断是否已遍历完所有记录。 - 对于`BULK COLLECT`,当`var_rowid.COUNT=0`时,表示已没有更多数据可以获取。 通过上述详细介绍,我们可以看到...

Global site tag (gtag.js) - Google Analytics