当前位置:网站首页>数据库高级学习笔记--游标
数据库高级学习笔记--游标
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;
边栏推荐
- [multithreading] the underlying principle of println method
- MATLAB的符号运算
- 就这么一个简单的校验,80%的程序员却做不到,更不理解!
- [Guangxi University] information sharing of postgraduate entrance examination and re examination
- [JVM] JVM refers to floating point number
- Regular expressions are hexadecimal digits?
- 478-82(56、128、718、129)
- 对话MySQL之父:代码一次性完成才是优秀程序员
- Detailed introduction of v-bind instruction
- SD卡介绍(基于SPEC3.0)
猜你喜欢

2022 safety officer-b certificate examination simulated 100 questions and answers

Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022

MySQL中各类型文件详解

21天学习挑战赛-《Autosar从入门到精通-实战篇》

Leetcode - hashtable topic

对话MySQL之父:代码一次性完成才是优秀程序员

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

业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)
![[Download] several tools for brute force cracking and dictionary generation are recommended](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[Download] several tools for brute force cracking and dictionary generation are recommended

【广西大学】考研初试复试资料分享
随机推荐
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
ARouter源码解析(二)
ActivityRouter源码解析
How to use gbase C API in multithreaded environment?
网络工程——软科中国大学专业排名
[swintransformer source code reading II] window attention and shifted window attention
Express builds a simple local background (1)
HashSet内部原理解析
Window source code analysis (IV): window deletion mechanism
可以伸缩的搜索栏,模仿华为应用市场
2022 safety officer-b certificate examination simulated 100 questions and answers
ECCV 2022 | 无需微调即可推广!基于配准的少样本异常检测框架
19c sysaux tablespace sqlobj$plan table is too large. How to clean it up
数据泄漏、删除事件频发,企业应如何构建安全防线?
【AUTOSAR-RTE】-2-Composition,Component和VFB的介绍
Face warp - hand tear code
Window源码解析(四):Window的删除机制
【解决】ERROR in [eslint] ESLint is not a constructor
LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符
Hexadecimal representation of negative numbers