10

关于游标(特别是 Oracle 游标)的快速问题。

假设我有一个名为“my_table”的表,它有两列,一个 ID 和一个名称。有数百万行,但名称列始终是字符串“test”。

然后我运行这个 PL/SQL 脚本:

declare
 cursor cur is
  select t.id, t.name
    from my_table t
   order by 1;
 begin
   for cur_row in cur loop
     if (cur_row.name = 'test') then
        dbms_output.put_line('everything is fine!');
     else
        dbms_output.put_line('error error error!!!!!');
        exit;
     end if;
   end loop;
 end; 
 /

如果我在运行时运行此 SQL:

 update my_table 
   set name = 'error'
  where id = <max id>;
commit;

PL/SQL 块中的光标会拾取该更改并打印出“错误错误错误”并退出吗?或者它根本不会接受更改......或者它甚至会允许更新到 my_table?

谢谢!

4

1 回答 1

15

游标有效地运行 SELECT,然后让您遍历结果集,该结果集保存在数据库状态的快照中。因为您的结果集已经被提取,它不会受到 UPDATE 语句的影响。(否则,每次移动光标时都需要重新运行查询!)

看:

http://www.techonthenet.com/oracle/cursors/declare.php

于 2009-11-06T22:13:34.647 回答