当前位置:网站首页>On cursor in MySQL
On cursor in MySQL
2022-06-11 06:21:00 【Milo_】
What is a cursor ?
The cursor (cursor) Is a store in MySQL Database query on server , It's not one SELECT sentence , But the result set retrieved by the statement . In the store After bid , The application can scroll or browse the data as needed .
Be careful :MySQL Cursors can be used for stored procedure , function , trigger , Incident
Cursor properties
- The database can also choose not to copy the result set
- Not updatable
- The cursor can only travel in one direction , And you can't skip any line of data . To use a cursor , First define a cursor variable
Create cursors
Before creating a cursor , We need to clear the syntax of the cursor first
1、 Define cursors
DECLARE Cursor name CURSOR FOR SQL sentence ;
2、 Open cursor
OPEN Cursor name ;
3、 To get the results
FETCH Cursor name INTO Variable name [, Variable name ];
4、 Close cursor
CLOSE Cursor name ;
We use Customers Table as an example

Example 1
Define a stored procedure , When called, execute the cursor inside
CREATE PROCEDURE PROC1()
BEGIN
-- Define two variables to store the results
DECLARE NAME VARCHAR(20);
DECLARE ADDR VARCHAR(50);
-- declare cursor
DECLARE MY CURSOR FOR SELECT full name , Address FROM customers;
-- Open cursor
OPEN MY;
-- To get the results
FETCH MY INTO NAME,ADDR;
-- Here is to show the obtained results
SELECT NAME,ADDR;
-- Close cursor
CLOSE MY;
END;
After we execute the above stored procedure , You can call the stored procedure
CALL PROC1();
Get the results :

There must be some friends here who are curious ,customers It's on the outside 7 Bar record , Why only 1 Bar record ?
This is because the variables of the cursor only retain customers The first row of data in the table , If you want to view the following data , You need to cycle down the cursor , To continue viewing .
Example 2
Define a stored procedure , When calling a stored procedure , Will table customers The data in the table is written to the new table .
CREATE PROCEDURE PROC2()
BEGIN
-- Define two variables to store the results
DECLARE FLAG INT DEFAULT 0;
DECLARE NAME VARCHAR(20);
DECLARE ADDR VARCHAR(50);
-- declare cursor
DECLARE MY CURSOR FOR SELECT full name , Address FROM customers;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET FLAG=1;
-- Open cursor
OPEN MY;
-- Circulating body part
L1:LOOP
-- To get the results
FETCH MY INTO NAME,ADDR;
IF FLAG=1 THEN
LEAVE L1;
END IF;
-- Here is to show the obtained results
INSERT INTO cus VALUES(NAME,ADDR);
-- Close cursor
END LOOP; -- End of cycle
CLOSE MY;
END;
Then we execute the stored procedure , And query cus Data in table
CALL PROC2();SELECT * FROM cus;
result :

Results and customers The same in , But these results are inserted in the process of moving down one by one , That is, the loop executes 7 Time .
These are the basic operating principles of cursors , In addition, the loop body of the cursor has WHILE,REPEAT Etc , They operate in the same way LOOP similar , Are used to loop the contents of the loop body , Until the end of the cycle .
Use scenarios
When we go through sql When querying data ,
1, The common way is to sql In the past , The server returns all the data to you , Another way is
2, Cursor mode , The cursor method will find the data to be queried on the server side , Then return it to you in batches , This method is suitable for scenarios where a large amount of operation data is to be operated , The server gave me a message , I deal with one , Then until the end of the loop processing , client You won't receive all the data at once
In the first way, there will be memory leakage ,
The second is not , But it takes up server links for a long time
Cursors are simply understood as : Tell the server the data result set I want to query , Then return to me in batches and deal with it slowly
~ complete
边栏推荐
- What is sentinel produced by Ali?
- How to use the markdown editor
- Who is stronger, zip or 7-Zip, and how to choose?
- Shandong University machine learning experiment 7 pca+ SVM face recognition
- Functional interface lambda, elegant code development
- Login and registration based on servlet, JSP and MySQL
- FPGA面試題目筆記(四)—— 序列檢測器、跨時鐘域中的格雷碼、乒乓操作、降低靜動態損耗、定點化無損誤差、恢複時間和移除時間
- Eureka cluster setup
- Sqoop installation tutorial
- Training and testing of super score model in mmediting
猜你喜欢

Servlet

SQLI_ LIBS range construction and 1-10get injection practice

Sign for this "plug-in" before returning home for the new year

Free get | full function version of version control software

学好C语言从关键字开始

FPGA设计——乒乓操作实现与modelsim仿真

Using Internet of things technology to accelerate digital transformation

Box model

Deployment of Flink

通过两种方式手写一个消息队列
随机推荐
Vulnhub's breach1.0 range exercise
Sqli-libs range 23-24 filtration and secondary injection practice
Warmly celebrate that yeyanxiu, senior consultant of Longzhi, won the title of "atlassian Certified Expert"
FPGA面试题目笔记(四)—— 序列检测器、跨时钟域中的格雷码、乒乓操作、降低静动态损耗、定点化无损误差、恢复时间和移除时间
Zvuldrill installation and customs clearance tutorial
Linux Installation redis
Metasploitabile2 target learning
ThymeleafEngine模板引擎
FPGA Design -- ping pong operation implementation and Modelsim simulation
Servlet
The classification effect of converting video classification data set to picture classification data set on vgg16
跨境电商测评自养号团队应该怎么做?
Squid agent
End of 2021 graphics of Shandong University
Compliance management 101: processes, planning and challenges
Instanceof and type conversion
How to treat the ethical issues arising from driverless Technology
MATLAB realizes mean filtering and FPGA for comparison, and uses Modelsim waveform simulation
Eureka cluster setup
Twitter data collection (content, fans, keywords, etc.)