当前位置:网站首页>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
边栏推荐
- 亚马逊、速卖通、Lazada、虾皮平台在用911+VM的环境可以进行产号、养号、补单等操作吗?
- Vulhub 8.1-backdoor vulnerability recurrence
- Print sparse arrays and restore
- Graphsage paper reading
- What is a planning BOM?
- 通过两种方式手写一个消息队列
- Super explanation
- 使用Meshlab对CAD模型采样点云,并在PCL中显示
- PgSQL reports an error: current transaction is aborted, commands ignored until end of transaction block
- Simple understanding of pseudo elements before and after
猜你喜欢

FPGA Design -- ping pong operation implementation and Modelsim simulation

SQLI_ LIBS range construction and 1-10get injection practice

Moteur de modèle de moteur thymeleaf

Graphsage paper reading

MATLAB realizes mean filtering and FPGA for comparison, and uses Modelsim waveform simulation

FPGA设计——乒乓操作实现与modelsim仿真
![[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)](/img/d6/e61ba5bad2b2847378c4547ce0780d.jpg)
[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)

Do you know the functions of getbit and setbit in redis?

Convert multiple pictures into one NPY file storage

Servlet
随机推荐
Markdown + typora + picgo experimental report template attached
End of 2021 graphics of Shandong University
Servlet
021-MongoDB数据库从入门到放弃
All the benefits of ci/cd, but greener
FPGA面试题目笔记(三)——跨时钟域中握手信号同步的实现、任意分频、进制转换、RAM存储器等、原码反码和补码
Examinelistactivity of Shandong University project training
亚马逊、速卖通、Lazada、虾皮平台在用911+VM的环境可以进行产号、养号、补单等操作吗?
URL in flask_ for
箭头函数的this指向
Shandong University machine learning experiment 7 pca+ SVM face recognition
不同VLAN间的通信
Zvuldrill installation and customs clearance tutorial
Compliance management 101: processes, planning and challenges
Quantitative understanding (Quantitative deep revolutionary networks for effective information: a whitepaper)
跨境电商测评自养号团队应该怎么做?
Shandong University machine learning final 2021
Shandong University machine learning experiment VI k-means
go的fmt包使用和字符串的格式化
A multi classification model suitable for discrete value classification -- softmax regression