一.Oracle的Cursors概念:
游标:用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作。
二.Oracle的Cursors分类:
1.静态游标:
分为显式游标和隐式游标。
2.REF游标:
是一种引用类型,类似于指针。
三.Oracle的Cursors详细内容:
1.显式游标:
CURSOR游标名(参数) [返回值类型] IS
Select语句
生命周期:
a.打开游标(OPEN)
解析,绑定。。。不会从数据库检索数据
b.从游标中获取记录(FETCH INTO)
执行查询,返回结果集。通常定义局域变量作为从游标获取数据的缓冲区。
c.关闭游标(CLOSE)
完成游标处理,用户不能从游标中获取行。还可以重新打开。
选项:参数和返回类型
set serveroutput on
declare
cursor emp_cur ( p_deptid in number) is
select * from employees where department_id = p_deptid;
l_emp employees%rowtype;
begin
dbms_output.put_line('Getting employees from department 30');
open emp_cur(30);
loop
fetch emp_cur into l_emp;
exit when emp_cur%notfound;
dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');
dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);
end loop;
close emp_cur;
dbms_output.put_line('Getting employees from department 90');
open emp_cur(90);
loop
fetch emp_cur into l_emp;
exit when emp_cur%notfound;
dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');
dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);
end loop;
close emp_cur;
end;
/
2.隐式游标:
不用明确建立游标变量,分两种:
a.在PL/SQL中使用DML语言,使用ORACLE提供的名为SQL的隐示游标
b.CURSOR FOR LOOP,用于for loop语句
a.举例:
declare
begin
update departments set department_name=department_name;
--where 1=2;
dbms_output.put_line('update '|| sql%rowcount ||' records');
end;
/
b.举例:
declare
begin
for my_dept_rec in ( select department_name, department_id from departments)
loop
dbms_output.put_line(my_dept_rec.department_id || ' : ' || my_dept_rec.department_name);
end loop;
end;
/
c.举例:
单独select
declare
l_empno emp.EMPLOYEE_ID%type;
-- l_ename emp.ename%type;
begin
select EMPLOYEE_ID
into l_empno
from emp;
--where rownum =1;
dbms_output.put_line(l_empno);
end;
/
使用INTO获取值,只能返回一行。
游标属性:
%FOUND:变量最后从游标中获取记录的时候,在结果集中找到了记录。
%NOTFOUND:变量最后从游标中获取记录的时候,在结果集中没有找到记录。
%ROWCOUNT:当前时刻已经从游标中获取的记录数量。
%ISOPEN:是否打开。
Declare
Cursor emps is
Select * from employees where rownum<6 order by 1;
Emp employees%rowtype;
Row number :=1;
Begin
Open emps;
Fetch emps into emp;
Loop
If emps%found then
Dbms_output.put_line('Looping over record '||row|| ' of ' || emps%rowcount);
Fetch emps into emp;
Row := row + 1;
Elsif emps%notfound then
Exit; ---exit loop, not IF
End if;
End loop;
If emps%isopen then
Close emps;
End if;
End;
/
显式和隐式游标的区别:
尽量使用隐式游标,避免编写附加的游标控制代码(声明,打开,获取,关闭),也不需要声明变量来保存从游标中获取的数据。
3.REF CURSOR游标:
动态游标,在运行的时候才能确定游标使用的查询。分类:
1.强类型(限制)REF CURSOR,规定返回类型
2.弱类型(非限制)REF CURSOR,不规定返回类型,可以获取任何结果集。
TYPE ref_cursor_name IS REF CURSOR [RETURN return_type]
Declare
Type refcur_t is ref cursor;
Type emp_refcur_t is ref cursor return employee%rowtype;
Begin
Null;
End;
/
强类型举例:
declare
--声明记录类型
type emp_job_rec is record(
employee_id number,
employee_name varchar2(50),
job_title varchar2(30)
);
--声明REF CURSOR,返回值为该记录类型
type emp_job_refcur_type is ref cursor
return emp_job_rec;
--定义REF CURSOR游标的变量
emp_refcur emp_job_refcur_type;
emp_job emp_job_rec;
begin
open emp_refcur for
select e.employee_id,
e.first_name || ' ' ||e.last_name "employee_name",
j.job_title
from employees e, jobs j
where e.job_id = j.job_id and rownum < 11 order by 1;
fetch emp_refcur into emp_job;
while emp_refcur%found loop
dbms_output.put_line(emp_job.employee_name || '''s job is ');
dbms_output.put_line(emp_job.job_title);
fetch emp_refcur into emp_job;
end loop;
end;
/
本篇文章来源于 PHP资讯 原文链接:http://www.phpq.net/oracle/oracle-cursors-syntax.html
分享到:
相关推荐
设有学生表student(学号sno, 姓名sname, 年龄 sage,系别dept)、课程表Course(课程号cno,课程名称cname)和选课表sc(学号sno,课程号cno,成绩grade),用Oracle PL/SQL语言设计一个名为pkg_student程序包,要求: ...
描述Oracle中游标的基本知识,纯英文,国外大学教授使用的教学笔记
Windows-8-Aero-Cursors_Black Windows 8 Aero 光标黑色
oracle参数open_cursors与session_cached_cursors详解.pdf
其他好看的主题请发邮件到hnjd314053754@163.com 我再转发
名称:Custom Cursors ---------------------------------------- 版本:1.1.1 作者:Custom Cursors 分类:网页增强 ---------------------------------------- 概述:让我们为您的日常浏览带来一些特别的东西。...
游标-任何网页上的多人绘图在网页上与多个用户一起绘制。 连接到node.js服务器。设置首先运行npm install 。 将config.sample.js复制到config.js并更改变量。 Gulp用于缩小JavaScript文件。 运行gulp进行构建。...
Win8 Aero Cursors Black:酷黑版Win8指针
Use Of Cursors And Parallel Cursors In SAP ABAP
MoonShine_Cursors 图形用户界面光标Windows 版本: Linux 版本:很快指针大小=32 xhot=3 yhot=7帮助大小=32 xhot=3 yhot=7 jifs 10 10 10 10 毫秒延迟167 167 167 167工作大小=32 xhot=6 yhot=4 jifs 15 15 15 30 7...
*.open_cursors=300 *.processes=150 *.remote_login_passwordfile='EXCLUSIVE' *.undo_tablespace='UNDOTBS1' 4、创建实例与密码文件 oradim -new -sid mydb -startmode manual -intpwd mydb -pfile E:\app...
图表上可以有多个光标此插件基于另一个插件jQuery.flot.crosshair.js ,可在的浮动图表包中选项该插件支持以下选项: cursors : [ { name : 'string' mode : null or 'x' or 'y' or 'xy' , color : color , ...
光标 Mac 原生光标元素的集合。 主要是 PDF 格式,但也有一些 PNG 文件。
oracle数据库忽然连不上了,一查是游标数量超了。 1.查看游标数 show parameter open_cursors 2.修改游标数 alter system set open_cursors = 10000; 3.获取打开的游标数 select o.sid, osuser, machine, count...
SVG,PNG,PNG @ 2x中的所有macOS光标,以及用于编辑的Sketch文件。 感谢和向我展示了如何获得这些东西。 执照 该软件根据。
Vimix游标这是x光标主题,其灵感来自Materia设计,并基于。安装要安装光标主题,只需将已编译的主题复制到您的icons目录。对于本地用户安装: ./install.sh对于所有用户的全系统安装: sudo ./install.sh然后使用您...
McMojave游标 这是一个受macOS启发并基于的x光标主题。 安装 要安装光标主题,只需将已编译的主题复制到您的icons目录。 对于本地用户安装: ./install.sh 对于所有用户的全系统安装: sudo ./install.sh ...
§3.4.22 打开的光标数(OPEN_CURSORS) 77 §3.4.23 优化方式(OPTIMIZER_MODE) 77 §3.4.24 进程数(PROCESSES) 77 §3.4.25 回滚段名称(ROLLBACK_SEGMENTS) 78 §3.4.26 服务名(SERVICE_NAMES) 78 §3.4.27 会话的数...
Windows Cursors 光标 文件 左撇子 左手用的
有哪些初始化参数最影响Oracle系统的性能?具体影响Oracle系统性能的初始化参数有:SGA(系统全局区);db_block_buffers;share_pool_size ;...open_cursors等等。 <?xml:namespace prefix = o /> 等等