怎么关闭oracle游标 oracle查询打开的游标

如何打开和关闭Oracle游标

--Oracle PL/SQL

成都创新互联公司专注于企业全网营销推广、网站重做改版、洛江网站定制设计、自适应品牌网站建设、H5技术商城建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为洛江等各大城市提供网站开发制作服务。

declare

--定义游标

cursor cur_test is

select * from emp;

v_emp emp%rowtype;

begin

--打开游标

open cur_test;

loop

--获取游标值

fetch cur_test

into v_emp;

exit when cur_test%notfound;--属性为是否提取数据成功,不成功则TRUE

dbms_output.put_line(v_emp.empno || '_' || v_emp.ename);

end loop;

--关闭游标

close cur_test;

end;

Oracle 游标

游标能够根据查询条件从数据表中提取一组记录,将其作为一个临时表置于数据缓冲区中,利用指针逐行对记录数据进行操作。

Oracle中的游标分为显示游标和隐式游标 。

在执行SQL语句时,Oracle会自动创建隐式游标,该游标是内存中处理该语句的数据缓冲区,存储了执行SQL语句的结果。通过隐式游标属性可获知SQL语句的执行状态信息。

%found:布尔型属性,如果sql语句至少影响到一行数据,值为true,否则为false。

%notfound:布尔型属性,与%found相反。

%rowcount:数字型属性,返回受sql影响的行数。

%isopen:布尔型属性,当游标已经打开时返回true,游标关闭时则为false。

用户可以显式定义游标。使用显式游标处理数据要4个步骤:定义游标、打开游标、提取游标数据和关闭游标。

游标由游标名称和游标对应的select结果集组成。定义游标应该放在pl/sql程序块的声明部分。

语法格式:cursor 游标名称(参数) is 查询语句

打开游标时,游标会将符合条件的记录送入数据缓冲区,并将指针指向第一条记录。

语法格式:open 游标名称(参数);

将游标中的当前行数据赋给指定的变量或记录变量。

语法格式:fetch 游标名称 into 变量名;

游标一旦使用完毕,就应将其关闭,释放与游标相关联的资源。

语法格式:close 游标名称;

declare

cursor c1 is  select sno,cno,grade from sc;

v_sno sc.sno%type;

v_cno sc.cno%type;

v_grade sc.grade%type;

begin

open c1;

loop

  fetch c1 into v_sno,v_cno,v_grade;

  exit when c1%notfound;--紧跟fetch之后

if c1%found then

dbms_output.put_line(to_char(c1%rowcount)||v_cno);

end if;

end loop;

close c1; 

end;

declare

cursor c1 is select sno,cno,grade from sc;

v_sno sc.sno%type;

v_cno sc.cno%type;

v_grade sc.grade%type;

begin

open c1;

fetch c1 into v_sno,v_cno,v_grade;

while c1%found loop

  dbms_output.put_line(v_sno||v_cno||v_grade);

 fetch c1 into v_sno,v_cno,v_grade;

end loop;

close c1; 

end;

第三种:for

declare

cursor c1 is select sno,cno,grade from sc;

begin

for item in c1 loop

dbms_output.put_line(rpad(item.sno,'10',' ')||rpad(item.cno,'10',' ')||rpad(item.grade,'10',' '));

end loop;

end;

Oracle游标sql语句代码块的优化

游标操作的优化:

-- 有一个表格,存储的是用户的名字,将 emp 表中所有的用户名转换成小写,再写入这个表格

create table ename_emp(

ename varchar2(50)

);

-- 下面这个游标的操作,需要 43s时间才能完成

declare

begin

for i in (select ename from emp_liebiao) loop

insert into ename_emp values(lower(i.ename));

commit;

end loop;

end;

-- 因为游标是以行为单位进行数据的操作的,所有游标的效率是比较慢的,而且游标需要消耗的内存也是比较多的,我们需要将游标以行进行操作的方式,修改成一次性操作所有数据的批量的方式。

批量操作在游标里面 叫做 bulk collect

第一步,创建一个表类型

type 表类型的名字 is table of 表名.列名 %type;

变量名 表类型的名字;

第三步,创建一个游标,读取某个查询的结果

cursor 游标名字 is select 查询语句 ;

第四步,打开游标

open 游标名字;

第五步,捕获游标的数据,将内容给到表类型的变量进行保存

fetch 游标名字 bulk collect into 变量名字 ;

第六步,使用 forall 语句,对数据进行批量的操作

forall i in 变量 .first .. 变量 .last DML 语句操作 ;

第七步,关闭游标

close 游标名字 ;

declare

-- 创建表类型

type biao is table of emp_liebiao.ename%type;

b biao;

-- 创建游标

cursor m is select ename from emp_liebiao;

begin

-- 打开游标

open m;

-- 将游标的内容批量的给到变量

fetch m bulk collect into b;

-- 使用forall批量修改数据

forall i in b.first .. b.last insert into ename_emp values(lower(b(i)));

commit;

-- 关闭游标

close m;

end;

练习:批量修改用户的编号,让编号+工资等级+部门,形成一个新的编号,存入下面的表格中,使用 bulk collect 来实现。

2000以下是 C ,2000-3000是 B ,3000以上是 A ,

例如 SMITH , 新编号应该是 7369_C_20

create table empno_emp(

empno varchar2(50)

);

declare

type biao is table of emp_liebiao%rowtype;

b biao;

cursor m is select * from emp_liebiao;

begin

open m;

fetch m bulk collect into b;

forall i in b.first..b.last

insert into empno_emp values(

b(i).empno||'_'||decode(sign(b(i).sal-2000)+sign(b(i).sal-3000),-2,'C',2,'A','B')||'_'||b(i).deptno

);

commit;

close m;

end;

oracle如何关闭游标?

1. 用open打开的,用close关闭\x0d\x0adeclare\x0d\x0acursor mycursor is\x0d\x0aselect * from emp for update;\x0d\x0amyrecord emp%rowtype;\x0d\x0abegin\x0d\x0aopen mycursor;\x0d\x0aloop\x0d\x0afetch mycursor into myrecord;\x0d\x0aexit when mycursor%notfound;\x0d\x0aif (myrecord.sal=2000) then\x0d\x0aupdate emp\x0d\x0aset sal=2001\x0d\x0awhere current of mycursor;\x0d\x0aend if;\x0d\x0aend loop;\x0d\x0aclose mycursor;\x0d\x0acommit;\x0d\x0aend;\x0d\x0a2. 用for 循环的,循环完了就自己关了\x0d\x0adeclare\x0d\x0acursor mycursor is\x0d\x0aselect * from emp;\x0d\x0abegin\x0d\x0afor i in mycursor\x0d\x0aloop\x0d\x0adbms_output.put_line(i.job);\x0d\x0aend loop;\x0d\x0aend;

当前文章:怎么关闭oracle游标 oracle查询打开的游标
标题来源:https://www.cdcxhl.com/article46/hisihg.html

成都网站建设公司_创新互联,为您提供网站制作网站收录品牌网站制作Google网站内链响应式网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化