当前位置:网站首页>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
边栏推荐
- Sqoop installation tutorial
- FPGA Design -- ping pong operation implementation and Modelsim simulation
- MySQL implements over partition by (sorting the data in the group after grouping)
- Learn C language well from keywords
- Summarize the five most common BlockingQueue features
- JIRA software annual summary: release of 12 important functions
- Moteur de modèle de moteur thymeleaf
- Use of constructors
- Teach you to write word formula
- EasyGBS接入的设备视频直播突然全部无法播放是为什么?数据库读写不够
猜你喜欢

jenkins-凭证管理

Can Amazon, express, lazada and shrimp skin platforms use the 911+vm environment to carry out production number, maintenance number, supplement order and other operations?

Sqoop installation tutorial

Using Internet of things technology to accelerate digital transformation

通过两种方式手写一个消息队列

Jenkins voucher management

Matlab实现均值滤波与FPGA进行对比,并采用modelsim波形仿真

Eureka集群搭建

FPGA interview notes (III) -- implementation of handshake signal synchronization in cross clock domain, arbitrary frequency division, binary conversion, RAM memory, original code inversion and complem

A multi classification model suitable for discrete value classification -- softmax regression
随机推荐
Ethical discussion on reptile Technology
Using Internet of things technology to accelerate digital transformation
Make a small game with R language and only basic package
Shandong University machine learning experiment 5 SVM
Sqli-labs less-01
ERROR 1215 (HY000): Cannot add foreign key constraint
jenkins-凭证管理
Wechat applet (authorized login of TP5)
Graphsage paper reading
Squid agent
How to use perforce helix core with CI build server
Sqli-libs range 23-24 filtration and secondary injection practice
Observer mode (listener mode) + thread pool to realize asynchronous message sending
Installing MySQL for Linux
Review XML and JSON
Build the first power cloud platform
What should the cross-border e-commerce evaluation team do?
Print sparse arrays and restore
Autojs, read one line, delete one line, and stop scripts other than your own
All the benefits of ci/cd, but greener