当前位置:网站首页>数据库高级学习笔记--游标
数据库高级学习笔记--游标
2022-07-28 09:06:00 【萍果馅是年糕】
--1.什么是游标
游标:用于临时存储一个查询返回的多行数据(结果集,类似于Java的Jdbc连接返回的ResultSet集合),
通过遍历游标,可以逐行访问处理该结果集的数据。
--2.游标有什么作用
①指定结果集中特定行的位置。
②基于当前的结果集位置检索一行或连续的几行。
③在结果集的当前位置修改行中的数据。
④对其他用户所做的数据更改定义不同的敏感性级别。
⑤可以以编程的方式访问数据库。
--3.要避免使用游标
因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该改写;
如果使用了游标,就要尽量避免在游标循环中再进行表连接的操作。
--4.游标分类
游标:静态游标 和 动态游标
--4.1静态游标
静态游标 分为 显示游标 和 隐式游标
4.1.1显示游标
用户显示声明的游标,即指定结果集。当查询返回结果超过一行时,就需要一个显式游标。
用户自己写的sql语句,编译时能明确知道sql语句
4.1.2隐式游标
所有DML语句(增、删、改、查询单条记录)为隐式游标。
该变量名不需要用户自己声明,它由系统帮我们定义,叫sql。使用时不需要声明隐式游标,它由系统定义 .
--4.2动态游标
在执行前不知道sql语句游标,执行时才知道sql语句的游标。 动态关联结果集的临时对象。
--5.游标的语法结构
游标:用来处理使用select语句从数据库中检索到的多行记录的工具。
--(我的理解:select查询出来几行数据会直接返回给用户,但是在用户需要对返回的每一条数据进行不同操作时,
--通过结果集来接收每一条数据,而游标像临时结果集能在结果集中像光标一样,能移动的不同位置,进行改变)
语法:cursor 游标名称(参数 参数类型) [return 返回游标的类型] is select语句 --(注意:需要处理的select语句,不能含INTO子句)
打开游标: open 游标名称
提取游标: fetch 游标名称 into v_list --注意:v_list必须与游标提取的结果集类型相同
关闭游标: close 游标名称; --注意:关闭游标后,所有资源都将被释放,且不能再次被打开
游标的使用方式:声明--->打开--->读取--->关闭
--注意:
读取游标:
Loop
Fetch 游标名称 into 变量
Exit when 游标名称%notfound
End loop;
declare
cursor cursor_fx1 is
select ename,sal from emp;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open cursor_fx1;
loop
fetch cursor_fx1 into v_ename,v_sal; --使用fetch语句只能读取一行记录
dbms_output.put_line(v_ename||v_sal);
exit when cursor_fx1%notfound;
end loop;
close cursor_fx1;
end;
--循环 使用fetch语句只能读取一行记录,对于多行数据需要循环
1.while
while t_dept%found loop
fetch t_dept into temp;
dbms_output.put_line(temp.dname);
end loop;
2.loop
Loop
Fetch 游标名称 into 变量
Exit when 游标名称%notfound
End loop;
3.for --自动打开游标,而无需使用OPEN语句,当循环结束后,游标会自动关闭
for 临时起的变量名 in 游标名称 loop
ename:=temp_emp.ename;
job:=temp_emp.job;
dbms_output.put_line(ename||' '||job);
end loop;
--带参数的游标
例:--使用游标查询并打印某部门的员工的姓名和薪资,部门编号为运行时手动输入。
--loop
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
cursor cursor_fx2(v_deptno emp.deptno%type) is
select ename,sal from emp where deptno=v_deptno;
begin
open cursor_fx2(10); ----10即为部门编号
loop
fetch cursor_fx2 into v_ename,v_sal;
dbms_output.put_line(v_ename||v_sal);
exit when cursor_fx2%notfound ;
end loop;
close cursor_fx2;
end;
-for
declare
v_ename varchar2(20); ---注意
v_sal varchar2(20);
cursor cursor_fx3(v_deptno emp.deptno%type) is
select ename,sal from emp where deptno=v_deptno;
begin
for xh1 in cursor_fx3(10) loop
v_ename:=xh1.ename;
v_sal:=xh1.sal;
dbms_output.put_line(v_ename||v_sal);
end loop;
end;
边栏推荐
- LinkedList内部原理解析
- ARouter源码解析(三)
- QT基础练手小程序-简单计算器设计(附带源码,解析)
- [multithreading] the underlying principle of println method
- 【一花一世界-郑一教授-繁简之道】可解释神经网络
- MATLAB启动慢解决措施
- Which system table is the keyword of SQL Server in?
- Informatics Olympiad all in one 1617: circle game | 1875: [13noip improvement group] circle game | Luogu p1965 [noip2013 improvement group] circle game
- C# 之 方法参数传递机制
- opencv安装配置测试
猜你喜欢
![[one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network](/img/fd/8ae7c00061491ad78a0fd68b7c21b0.png)
[one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network
![Magic brace- [group theory] [Burnside lemma] [matrix fast power]](/img/cf/606d1bc7cd877771afbdd7640b718c.png)
Magic brace- [group theory] [Burnside lemma] [matrix fast power]

【日志】日志干什么的?日志工厂是什么?log4j 的配置和使用? log4j.properties 文件配置、log4j jar包坐标

go语言切片Slice和数组Array对比panic runtime error index out of range问题解决
![[swintransformer source code reading II] window attention and shifted window attention](/img/fb/5273d87fed66c75a92aec8e94980a3.png)
[swintransformer source code reading II] window attention and shifted window attention

【C语言】详解顺序表(SeqList)

2022 high voltage electrician examination simulated 100 questions and simulated examination

Leetcode - hashtable topic

How promise instance solves hell callback

2022 high voltage electrician examination simulated 100 questions and simulated examination
随机推荐
[Guangxi University] information sharing of postgraduate entrance examination and re examination
376. 摆动序列【贪心、动态规划------】
With such a simple verification, 80% of programmers can't do it, let alone understand it!
Window源码解析(二):Window的添加机制
Alibaba cloud server setup and pagoda panel connection
Analysis of HashSet internal principle
数据库那么多概念性的东西怎么学?求方法
LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符
How promise instance solves hell callback
《我的Vivado实战—单周期CPU指令分析》
【AUTOSAR-RTE】-2-Composition,Component和VFB的介绍
Window source code analysis (IV): window deletion mechanism
Oracle-11gr2 default system job
IT行业数据与应用关系的变迁
[high number] high number plane solid geometry
数据库核心体系
How view works
什么是跨域?如何解决请跨域问题?
Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
【广西大学】考研初试复试资料分享