当前位置:网站首页>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;
边栏推荐
- Ten thousand words detailed Google play online application standard package format AAB
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- R语言-用于非平衡数据集的一些度量指标
- Machine learning strong foundation plan 0-5: why is the essence of learning generalization ability?
- STM32 drives st7701s chip (V ⅰ V0 mobile phone screen change price)
- How to deal with invalid objects monitored by Oracle every day in the production environment?
- I/O实操之对象流(序列化与反序列化)
- Understand several concepts of Oracle
- Random talk on GIS data (V) - geographic coordinate system
- 我想请教下各位大佬,cdc采集mysql时,如果发生了主从切换,有什么方案可以处理吗
猜你喜欢

Introduction to web security RADIUS protocol application
![[half understood] zero value copy](/img/4b/c8140bf7ee4baa094ca3011108d686.gif)
[half understood] zero value copy

融云 IM & RTC 能力上新盘点

天狼星网络验证源码/官方正版/内附搭建教程

大三下学期总结

Solutions to the disappearance of Jupiter, spyder, Anaconda prompt and navigator shortcut keys

什么样的知识付费系统功能,更有利于平台与讲师发展?

对话庄表伟:开源第一课
![[极客大挑战 2019]BabySQL-1|SQL注入](/img/21/b5b4727178a585e610d743e92248f7.png)
[极客大挑战 2019]BabySQL-1|SQL注入

leetcode:1300. 转变数组后最接近目标值的数组和【二分】
随机推荐
【MySQL】Got an error reading communication packets
Ripro9.0 revised and upgraded version +wp two beautification packages + rare plug-ins
Summary of common RSA related problems in CTF: basic RSA encryption and decryption
「学习笔记」树状数组
zotero文献管理器及其使用姿势(不定时更新)
完整版H5社交聊天平台源码[完整数据库+完整文档教程]
Good use explosion! The idea version of postman has been released, and its functions are really powerful
重新刷新你对Redis集群的理解
2021-03-24
什么是WordPress
擦黑板特效表白H5源码+非常浪漫/附BGM
What is the process of switching c read / write files from user mode to kernel mode?
Blackboard cleaning effect shows H5 source code + very romantic / BGM attached
A solution to the problem that ThinkPad fingerprint verification cannot be used in win7
Object to object mapping -automapper
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
WinForm generates random verification code
b2子主题/博客b2child子主题/开源源码
Using C language to compile student achievement management system (C language student achievement management system deleted)
【cesium】entity属性和时许绑定:SampledProperty方法简单使用