当前位置:网站首页>[cursor nesting] nesting of MySQL stored procedure cursors
[cursor nesting] nesting of MySQL stored procedure cursors
2022-06-22 17:12:00 【Seven days at night】
We use mysql When storing a procedure Sometimes we go through a lot of data , So selecting a cursor is a good way It will help us get the data . But most of the time , We will need the combination of nested cursors to solve the problem of
problem . Okay, talk less , Let's start with a simple cursor :
// Define cycle markers
DECLARE useraccountid INT DEFAULT 0; # The user id
DECLARE done TINYINT(1) DEFAULT 0;
// The account number in the query table Storing variables cur1 in
DECLARE cur1 CURSOR FOR SELECT user_account_id FROM user_account_info ';
# Basically everyone who uses a cursor has this sentence
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; # Flag for loop termination , If there is no data in the cursor, set done by 1
# Open cursor
OPEN cur1;
FETCH cur1 INTO useraccountid;
WHILE (done=0) DO
FETCH cur1 INTO useraccountid;
// Logical processing
// End of cycle
END WHILE ;
// Close cursor
CLOSE cur1;
The one above is a simple one Cursor template , When there is no data in the cursor done=1 Exit loop .
Nested cursors are needed today I found many examples on the Internet , Basically I can't understand Because I am also the first time to contact this . Since stored procedures can also be understood as a new programming language , So language It should be the same .
With the help of java The nested loop problem of language , So can cursors be implemented in this way . Let's try to see if we can
DECLARE useraccountid int DEFAULT 0;
DECLARE daiid int DEFAULT 0;
DECLARE done TINYINT(1) DEFAULT 0;# Is the data tag not found
DECLARE cur1 CURSOR FOR select agent_account_id from agent_account_info where parent_id in ( select agent_account_id from agent_account_info where grandpa_id in (select agent_account_id from agent_account_info where agent_account=CONCAT(agentAcc)) or parent_id in (select agent_account_id from agent_account_info where agent_account=CONCAT(agentAcc)) or agent_account=CONCAT(agentAcc)) or agent_account=CONCAT(agentAcc);
DECLARE cur2 CURSOR For select user_account_id from user_account_info WHERE user_account_type=2 and parent_id=CONCAT(daiid);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; # Flag for loop termination , If there is no data in the cursor, set done by 1
# Open cursor
OPEN cur1;
FETCH cur1 INTO daiid;# get data
WHILE done=0 DO
open cur2;
fetch cur2 into useraccountid;
WHILE done=0 DO
fetch cur2 into useraccountid;
END WHILE ;
CLOSE cur2;
SET done = 0; // Just remember this Others will be done according to the programming language you are familiar with
FETCH cur1 INTO daiid;# get data
END WHILE ;
CLOSE cur1;
If there's no above set done=0 Words By default, the inner loop flag is executed done=1 Will terminate the outer loop In other words, you can only perform one operation to push .
边栏推荐
- Quickly master asp Net authentication framework identity - login and logout
- Gridhome, a must-have static site generator for beginners
- Linux system maintenance: mysql8.0.13 source code download and installation "fool" operation steps (Linux centos6.8) test available series
- WPF achieves star effect
- You call this crap high availability?
- .NetCore实现图片缩放与裁剪 - 基于ImageSharp
- Review the executor from the perspective of application submission
- Service or mapper cannot be injected into a multithread
- mysql5.7.27安装之windows8.1 64
- 网传学习通1.7亿密码泄露!有什么补救措施?
猜你喜欢

Social responsibility: GAC Honda advocates children's road traffic safety in "dream children's travel"

linux系统维护篇:mysql8.0.13源码下载及安装之“傻瓜式”操作步骤(linux-centos6.8)亲测可用系列

What should I do if I can't hear a sound during a video conference?
![[Alibaba cloud server - install MySQL version 5.6 and reinstall]](/img/5a/50b1de5f58235f6d11f6ad1eecc455.png)
[Alibaba cloud server - install MySQL version 5.6 and reinstall]

快速掌握 ASP.NET 身份认证框架 Identity - 登录与登出

华为云招募工业智能领域合作伙伴,强力扶持+商业变现

web技术分享| 【高德地图】实现自定义的轨迹回放

High availability ResourceManager

Seeing the big from the small: a simple example of domain modeling, understanding "domain driven".

Apache ShardingSphere 一文读懂
随机推荐
Redis实现延迟队列的正确姿势
The MySQL of docker restarts, prompting error response from daemon: driver failed programming external connectivity on**
Analysis of the read data source code of spark shuffle
Iterators and generators
The way to optimize spark performance -- solving N poses of spark data skew
redis.clients.jedis.exceptions.JedisDataException ERR invalid password.
Basic application of scala for
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Quickly master asp Net authentication framework identity - user registration
On the closure function of Scala
WPF效果第一百九十篇之再耍ListBox
图计算Hama-BSP模型的运行流程
Blazor University (30)表单 —— 从 InputBase 派生
MySQL stored procedure exception handling error code: 1337
每秒处理10万高并发订单的乐视集团支付系统架构分享
C#-Linq源码解析之DefaultIfEmpty
Tried several report tools, and finally found a report based on Net 6
Problems and recovery of spark streaming checkpoint
视频会议时听不到声音该如何处理?
Team management | how to improve the thinking skills of technical leaders?