----------------sys_refcursor---------------
declare
cur sys_refcursor;
cur_e test1.id%type;
begin
open cur for
select id from test1;
insert into test1 (id) values (2); --cur读取不到新增加的数据2
commit;
loop
fetch cur into cur_e;
exit when cur %notfound;
dbms_output.put_line(cur_e);
end loop;
-----------------cursor-----------------
declare
cursor cur is select id from test1;
cur_e cur%rowype;
begin
insert into test1 (id)values(2);
commit;
for cur_e in cur loop --能读取新增加的数据2
exit when cur %notfound;
dbms_output.put_line(cur_e.id);
end loop;
end;
-----------------一致性读,可调式进行验证-------------
create or replace package body pkg as
procedure pro is
mycur sys_refcursor;
row_mycur mas%rowtype;
begin
open mycur for
select * from mas; --一致性读
loop
fetch mycur into row_mycur;
exit when mycur%notfound;
dbms_output.put_line(row_mycur.name);
end loop;
end;
end pkg;
declare
cur sys_refcursor;
cur_e test1.id%type;
begin
open cur for
select id from test1;
insert into test1 (id) values (2); --cur读取不到新增加的数据2
commit;
loop
fetch cur into cur_e;
exit when cur %notfound;
dbms_output.put_line(cur_e);
end loop;
-----------------cursor-----------------
declare
cursor cur is select id from test1;
cur_e cur%rowype;
begin
insert into test1 (id)values(2);
commit;
for cur_e in cur loop --能读取新增加的数据2
exit when cur %notfound;
dbms_output.put_line(cur_e.id);
end loop;
end;
-----------------一致性读,可调式进行验证-------------
create or replace package body pkg as
procedure pro is
mycur sys_refcursor;
row_mycur mas%rowtype;
begin
open mycur for
select * from mas; --一致性读
loop
fetch mycur into row_mycur;
exit when mycur%notfound;
dbms_output.put_line(row_mycur.name);
end loop;
end;
end pkg;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30201626/viewspace-1813749/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30201626/viewspace-1813749/
本文探讨了在SQL操作中使用sys_refcursor与cursor两种方法进行数据读取的差异,重点介绍了如何确保数据一致性及通过一致性读取进行验证的方法。
3030

被折叠的 条评论
为什么被折叠?



