当前位置:网站首页>Database advanced learning notes cursor
Database advanced learning notes cursor
2022-07-28 11:35:00 【Pingguo stuffed with rice cakes】
--1. What is a cursor
The cursor : Used to temporarily store multiple rows of data returned by a query ( Result set , Be similar to Java Of Jdbc Connection returned ResultSet aggregate ),
By traversing the cursor , The data processing the result set can be accessed row by row .
--2. What does a cursor do
① Specifies the location of a specific row in the result set .
② Retrieve a row or consecutive rows based on the current result set location .
③ Modify the data in the row at the current position of the result set .
④ Define different levels of sensitivity for data changes made by other users .
⑤ You can access the database programmatically .
--3. Avoid using cursors
Because the efficiency of cursors is poor , If the data of cursor operation exceeds 1 Line ten thousand , Then we should rewrite ;
If a cursor is used , Try to avoid the operation of table connection in the cursor loop .
--4. Cursor classification
The cursor : Static cursors and Dynamic cursors
--4.1 Static cursors
Static cursors It is divided into Show cursor and Implicit cursors
4.1.1 Show cursor
The user displays the declared cursor , That is, specify the result set . When the query returns more than one row , You need an explicit cursor .
It's written by users themselves sql sentence , When compiling, you can clearly know sql sentence
4.1.2 Implicit cursors
all DML sentence ( increase 、 Delete 、 Change 、 Query single record ) Is an implicit cursor .
The variable name does not need to be declared by the user , It is defined by the system , It's called sql. There is no need to declare an implicit cursor when using , It is defined by the system .
--4.2 Dynamic cursors
Don't know before implementation sql Statement cursor , I don't know until I execute sql The cursor of the statement . Temporary objects for dynamically associating result sets .
--5. The syntax of cursors
The cursor : Used to handle the use of select Statement to retrieve multiple rows of records from a database .
--( I understand it :select A few rows of data found will be directly returned to the user , But when the user needs to perform different operations on each piece of data returned ,
-- Receive every piece of data through the result set , Cursors, like temporary result sets, can act like cursors in result sets , Different positions that can be moved , To change )
grammar :cursor Cursor name ( Parameters Parameter type ) [return Return the type of cursor ] is select sentence --( Be careful : Need to be dealt with select sentence , Do not include INTO Clause )
Open cursor : open Cursor name
Extract cursor : fetch Cursor name into v_list -- Be careful :v_list Must be of the same type as the result set extracted by the cursor
Close cursor : close Cursor name ; -- Be careful : After closing the cursor , All resources will be released , And cannot be opened again
How to use cursors : Statement ---> open ---> Read ---> close
-- Be careful :
Read cursor :
Loop
Fetch Cursor name into Variable
Exit when Cursor name %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; -- Use fetch Statement can only read one line of record
dbms_output.put_line(v_ename||v_sal);
exit when cursor_fx1%notfound;
end loop;
close cursor_fx1;
end;
-- loop Use fetch Statement can only read one line of record , For multi row data, you need to cycle
1.while
while t_dept%found loop
fetch t_dept into temp;
dbms_output.put_line(temp.dname);
end loop;
2.loop
Loop
Fetch Cursor name into Variable
Exit when Cursor name %notfound
End loop;
3.for -- Automatically open the cursor , Without having to use OPEN sentence , When the cycle is over , The cursor will close automatically
for Temporary variable name in Cursor name loop
ename:=temp_emp.ename;
job:=temp_emp.job;
dbms_output.put_line(ename||' '||job);
end loop;
-- Cursors with parameters
example :-- Use the cursor to query and print the name and salary of employees in a department , The department number is entered manually during operation .
--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 It is the department number
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); --- Be careful
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;
边栏推荐
- Byte side: how to realize reliable transmission with UDP?
- mysql(8.0.16版)命令及说明
- 「Node学习笔记」Koa框架学习
- 本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
- What's the secret of creating a popular short video?
- [cesium] entity property and timing binding: the sampledproperty method is simple to use
- Rongyun IM & RTC capabilities on new sites
- 2021-03-24
- 一种比读写锁更快的锁,还不赶紧认识一下
- Three methods of using unity mouse to drive objects
猜你喜欢

Introduction to web security RADIUS protocol application

18张图,直观理解神经网络、流形和拓扑

目标检测领域必看的6篇论文
![完整版H5社交聊天平台源码[完整数据库+完整文档教程]](/img/3f/03239c1b4d6906766348d545a4f234.png)
完整版H5社交聊天平台源码[完整数据库+完整文档教程]

万字详解 Google Play 上架应用标准包格式 AAB

Quickly deploy mqtt clusters on AWS using terraform

对话庄表伟:开源第一课

机器学习强基计划0-5:为什么学习的本质是泛化能力?

字节一面:如何用 UDP 实现可靠传输?

Machine learning strong foundation plan 0-5: why is the essence of learning generalization ability?
随机推荐
mysql的左连接和右连接(内连接和自然连接的区别)
融云 IM & RTC 能力上新盘点
How to deal with invalid objects monitored by Oracle every day in the production environment?
什么样的知识付费系统功能,更有利于平台与讲师发展?
本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
WinForm generates random verification code
Leetcode:981. time based key value storage [trap of iteration for: on]
Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
【MySQL从入门到精通】【高级篇】(十)MyISAM的索引方案&&索引的优缺点
Leetcode:1300. the sum of the array closest to the target value after transforming the array [dichotomy]
go status. Go status code definition
1331. 数组序号转换
Full version of H5 social chat platform source code [complete database + complete document tutorial]
Matlab feature point extraction -- Record for self use
I/O实操之对象流(序列化与反序列化)
Ten thousand words detailed Google play online application standard package format AAB
Office2013 input mathematical formula above
No swagger, what do I use?
Detailed explanations of%*d,%.*s, etc. of [C language]: "recommended collection"
leetcode:1300. 转变数组后最接近目标值的数组和【二分】