oracle数据库游标(Oracle游标是什么请用自己话描述,谢谢大家了。)
老铁们,大家好,相信还有很多朋友对于oracle数据库游标和Oracle游标是什么请用自己话描述,谢谢大家了。的相关问题不太懂,没关系,今天就由我来为大家分享分享oracle数据库游标以及Oracle游标是什么请用自己话描述,谢谢大家了。的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
oracle 一个游标可存多大的数据量
游标和指针我理解是一个意思的
它只是用来描述取得内存中数据的方式
就像GPS导航一样,GPS会给你地图上的位置,而能不能跑到,取决与你的车里有多少油
例如:
在数据库中查询取到一个结果集你能不能取到那些数据,取决于你的机器假如结果集中有一个CLOB类型字段而你的机器,内存+硬盘总共250M没可能看到数据,对吧
修改最大游标: alter system set open_cursor=800, scope=both;
只要你能接受性能,随便你改,另外 open_cursor是整形改得越大,性能越受影响
以下来自“奔驰M888”的回答
1、plsql是面向过程的语言,这类语言还有c,cobol等,这类语言的共同点是一次只能处理一条数据,而数据库sql返回的对象是一个集合,这样直接用plsql程序操作就会出现问题。
2、在这种环境下就出现了游标,游标实际是一个内存地址,只想的是sql查询出的结果集,当需要的时候再根据游标一条一条取数据【fetch】,直到全部数据取完。
就像chsoftstar说的那样出现游标无法取数的问题,很大可能是写错了,具体什么原因,还得楼主自己查找了
Oracle游标是什么请用自己话描述,谢谢大家了。
一游标是什么
游标字面理解就是游动的光标。
用数据库语言来游标是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了,将游标放置到某行后,即可对该行数据进行操作,例如提取当前行的数据等。
二游标的分类
显式游标和隐式游标
显式游标的使用需要4步:
1.声明游标
CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no= vartype;
2.打开游标
open mycur(000627)
注:000627是参数
3.读取数据
fetch mycur into varno, varprice;
4.关闭游标
close mycur;
三游标的属性
oracle游标有4个属性:%ISOPEN,%FOUND,%NOTFOUND,%ROWCOUNT。
%ISOPEN判断游标是否被打开,如果打开%ISOPEN等于true,否则等于false;
%FOUND%NOTFOUND判断游标所在的行是否有效,如果有效,则%FOUNDD等于true,否则等于false;
%ROWCOUNT返回当前位置为止游标读取的记录行数。
四示例
set serveroutput on;
declare
varno varchar2(20);
varprice varchar2(20);
CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no= vartype;
begin
if mycur%isopen= false then
open mycur(000627);
end if;
fetch mycur into varno,varprice;
while mycur%found
loop
dbms_output.put_line(varno||','||varprice);
if mycur%rowcount=2 then
exit;
end if;
fetch mycur into varno,varprice;
end loop;
close mycur;
end;
PL/SQL记录的结构和C语言中的结构体类似,是由一组数据项构成的逻辑单元。
PL/SQL记录并不保存在数据库中,它与变量一样,保存在内存空间中,在使用记录时候,要首先定义记录结构,然后声明记录变量。可以把PL/SQL记录看作是一个用户自定义的数据类型。
set serveroutput on;
declare
type person is record
(
empno cus_emp_basic.emp_no%type,
empzc cus_emp_basic.emp_zc%type);
person1 person;
cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;
begin
if mycur%isopen= false then
open mycur(000627);
end if;
loop
fetch mycur into person1;
exit when mycur%notfound;
dbms_output.put_line('雇员编号:'||person1.empno||',地址:'||person1.empzc);
end loop;
close mycur;
end;
典型游标for循环
游标for循环示显示游标的一种快捷使用方式,它使用for循环依次读取结果集中的行数据,当form循环开始时,游标自动打开(不需要open),每循环一次系统自动读取游标当前行的数据(不需要fetch),当退出for循环时,游标被自动关闭(不需要使用close)。使用游标for循环的时候不能使用open语句,fetch语句和close语句,否则会产生错误。
set serveroutput on;
declare
cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;
begin
for person in mycur(000627) loop
dbms_output.put_line('雇员编号:'||person.emp_no||',地址:'||person.emp_zc);
end loop;
end;
Oracle数据库游标的类型
游标是SQL的一个内存工作区由系统或用户以变量的形式定义游标的作用就是用于临时存储从数据库中提取的数据块
Oracle数据库的Cursor类型包含三种静态游标分为显式(explicit)游标和隐式(implicit)游标 REF游标是一种引用类型类似于指针
测试数据
create table student(sno number primary key sname varchar())
declare i number:=;
beginwhile i<=
loop
insert into student(sno sname) values(i name||to_char(i))
i:=i+;
end loop;
end;
隐式游标属性
SQL%ROWCOUNT整型代表DML语句成功执行的数据行数
SQL%FOUND布尔型值为TRUE代表插入删除更新或单行查询操作成功
SQL%NOTFOUND布尔型与SQL%FOUND属性返回值相反
SQL%ISOPEN布尔型DML执行过程中为真结束后为假
declarebegin update student set sname= name||to_char(sno*) where sname= name;
if sql%found then
dbms_output put_line( name is updated)
else
dbms_output put_line(没有记录)
end if;
end;
declare
begin
for names in(select* from student) loop
dbms_output put_line(names sname)
end loop;
exception when others then
dbms_output put_line(sqlerrm)
end;
显式游标属性
%ROWCOUNT获得FETCH语句返回的数据行数
%FOUND最近的FETCH语句返回一行数据则为真否则为假
%NOTFOUND布尔型与%FOUND属性返回值相反
%ISOPEN布尔型游标已经打开时值为真否则为假
对于显式游标的运用分为四个步骤
a定义游标 Cursor [Cursor Name] IS;
b打开游标 Open [Cursor Name];
c操作数据 Fetch [Cursor name];
d关闭游标 Close [Cursor Name];
典型显式游标
declare cursor cur_rs is select* from student; sinfo student%rowtype;
begin open cur_rs;
loop
fetch cur_rs into sinfo;
exit when cur_rs%%notfound;
dbms_output put_line(sinfo sname)
end loop;
exception when others then
dbms_output put_line(sqlerrm)
end;
带参数open的显式cursor:
declare cursor cur_rs(in_name varchar) is select*
from student where sname=in_name;
begin for sinfo in cur_rs( sname) loop
dbms_output put_line(sinfo sname)
end loop;
exception when others then
dbms_output put_line(sqlerrm)
end;
使用current of语句执行update或delete操作
declare
cursor cur_rs is select* from student for update;
begin for sinfo in cur_rs loop
update student set sname=sname|| xx where current of cur_rs;
end loop;
mit;
exception when others then
dbms_output put_line(sqlerrm)
end;
REF游标用于处理运行时才能确定的动态sql查询结果利用REF CURSOR可以在程序间传递结果集(一个程序里打开游标变量在另外的程序里处理数据)
也可以利用REF CURSOR实现BULK SQL提高SQL性能
REF CURSOR分两种 Strong REF CURSOR和 Weak REF CURSOR
Strong REF CURSOR:指定retrun type CURSOR变量的类型必须和return type一致
Weak REF CURSOR:不指定return type能和任何类型的CURSOR变量匹配
运行时根据动态sql查询结果遍历
create or replace package pkg_test as
type student_refcursor_type is ref cursor return student%rowtype;
procedure student_rs_loop(cur_rs IN student_refcursor_type)
end pkg_test;
create or replace package body pkg_test as
procedure student_rs_loop(cur_rs IN student_refcursor_type) is
std student%rowtype;
begin loop
fetch cur_rs into std;
exit when cur_rs%NOTFOUND;
dbms_output put_line(std sname)
end loop;
end student_rs_loop;
end pkg_test;
declare stdRefCur pkg_test student_refcursor_type;
begin for i in loop
dbms_output put_line( Student NO=|| i)
open stdRefCur for select* from student where sno=i;
pkg_test student_rs_loop(stdRefCur)
end loop;
exception when others then dbms_output put_line(sqlerrm)
close stdRefCur;
end;
使用FORALL和BULK COLLECT子句利用BULK SQL可以减少PLSQL Engine和SQL Engine之间的通信开销提高性能
加速INSERT UPDATE DELETE语句的执行也就是用FORALL语句来替代循环语句
加速SELECT用BULK COLLECT INTO来替代INTO
create table
student_tmp as select sno
sname from student where=;
删除主键约束 alter table student drop constraint SYS_C;
执行两遍插入 insert into student select* from student where sno=;
declare cursor cur_std(stdid student sno%type) is select sno
sname from student where sno=stdid;
type student_table_type is table of cur_std%rowtype index by pls_integer;
student_table student_table_type;
begin
open cur_std()
fetch cur_std bulk collect into student_table;
close cur_std;
for i in unt loop
dbms_output put_line(student_table(i) sno||
|| student_table(i) sname)
end loop;
forall i in student_table firststudent_table last
insert into student_tmp values(student_table(i) sno student_table(i) sname)
mit;
end;
lishixinzhi/Article/program/Oracle/201311/17358好了,文章到这里就结束啦,如果本次分享的oracle数据库游标和Oracle游标是什么请用自己话描述,谢谢大家了。问题对您有所帮助,还望关注下本站哦!