当前位置:网站首页>MySQL cursors
MySQL cursors
2022-07-30 21:32:00 【m0_54853420】
1. Overview of cursors
In the sql statement we wrote before, although the returned records can be limited by filter conditions, we have no way to locate each record in the result set, like a pointer, forward positioning, backward positioning, or randomLocate a record.To solve this problem, we can use cursors.
The cursor allows us to locate each record in the result set and operate on the data in the pointed record
- In SQL, a cursor is a temporary database object
- The cursor acts as a pointer
- We can manipulate the data by manipulating the cursor
- Cursors in MySQL can be used in stored procedures and functions
2. Steps to use the cursor
- The cursor must be declared before the handler is declared, and variables and conditions must also be declared before the cursor or handler is declared
- Using a cursor generally goes through the following four steps:
- Declare a cursor
- Open cursor
- Using a cursor
- Close the cursor
Let's take a look at how to use it.
2.1 Declaring Cursors
In MySQL, use the declare keyword to declare a cursor
Syntax
DECLARE cursor name CURSOR FOR query statement;
The select statement here is used to return a result set that creates a cursor
cursor in English means cursor, cursor
Simple memory means using a query statement to describe the cursor
Example
DECLARE cur_emp CURSOR FOR
SELECT employee_id,salary FROM employees;
2.2 open cursor
After defining the cursor, if you want to use it, you must open the cursor
When the cursor is opened, the query result set of the select statement will be sent to the cursor work area to prepare for the subsequent cursor to read the records in the result set one by one
Syntax
open cursor name
2.3 Using a cursor (getting data from a cursor)
Syntax
fetch cursorname into var_name…
We use a cursor to read the current row, we can save the data to a variable, and the cursor pointer points to the next row
If the data read by the cursor has multiple columns, we can assign it to multiple variables after into
var_name should be defined before the cursor declaration
The number of fields in the result set of the cursor query must be the same as the number of variables after into, otherwise an error will be reported when the stored procedure is executed.
2.4 close cursor
- Syntax: close cursor name
- Because using a cursor will occupy system resources, it needs to be closed in time. If it is not closed in time, the cursor will be saved until the end of the stored procedure, which affects the efficiency of system operation. Closing the cursor can release the system resources occupied by the cursor.
- After closing the cursor, we can no longer retrieve the data rows in the query result. If we need to retrieve, we need to open the cursor again.
3. Case presentation
Create stored procedure "get_count_by_limit_total_salary()"
Declare IN parameter limit_total_salary, DOUBLE type; declare OUT parameter total_count, INT type.
The function of the function can realize the accumulation of the salary values of the employees with the highest salary until the total salary reaches the value of the limit_total_salary parameter, and returns the accumulated number to total_count.
DELIMITER //
CREATE PROCEDURE get_count_by_limit_total_salary(IN limit_total_salary DOUBLE,OUT total_count INT)
BEGIN
DECLARE sum_salary DOUBLE DEFAULT 0; #Record accumulatedTotal salary
DECLARE cursor_salary DOUBLE DEFAULT 0; #Record a salary value
DECLARE emp_count INT DEFAULT 0; #Record the number of loops
#Define cursor
DECLARE emp_cursor CURSOR FOR SELECT salary FROM employees ORDER BYsalary DESC;
#Open the cursor
OPEN emp_cursor;REPEAT#Use the cursor (get data from the cursor)FETCH emp_cursor INTO cursor_salary;SET sum_salary = sum_salary + cursor_salary;SET emp_count = emp_count + 1;UNTIL sum_salary >= limit_total_salaryEND REPEAT;SET total_count = emp_count;#Close the cursorCLOSE emp_cursor;
END //
DELIMITER ;
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 基于ABP实现DDD--领域逻辑和应用逻辑
- (7/29) Basic board minimum spanning tree prim+kruskal
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-
- 【深度学习】目标检测|SSD原理与实现
- Use the map function to operate on each element in the list It seems that you don't need a map
- 导航栏----个人中心 Dropdown
- MySql创建数据表
- Niu Ke Xiaobaiyue Race 53 A-E
- 登堂入室之soc开发makefile
- Markdown的使用
猜你喜欢
随机推荐
【Nacos】解决Nacos下载速度缓慢的问题
数据指标口径不统一、重复开发?亿信ABI指标管理平台帮你解决
WinDbg实践--入门篇
What is the common factor
走进Redis,让你重新认识redis。绝不是表面
The structure of knowledge in the corners of the C language
牛客网——业务分析-提取值
【机器学习】梯度下降背后的数学之美
Google Earth Engine ——ee.List.sequence函数的使用
【深度学习】对迁移学习中域适应的理解和3种技术的介绍
JS中获取元素属性的8大方法
【菜鸡含泪总结】如何用pip、anaconda安装库
DPW-SDNet: Dual Pixel-Wavelet Domain Deep CNNs for Soft Decoding of JPEG-Compressed Images
系统结构考点之多级混洗交换网络
导航栏----个人中心 Dropdown
大家都在用的plm项目管理软件有哪些
MySQL笔记1(数据库的好处,数据库的概念,数据库的特点,MySQL的启动,数据模型,SQL)
LeetCode · 23. Merge K ascending linked lists · recursion · iteration
系统结构考点之CRAY-1向量处理机
共用体、共用体与结构体的区别、枚举之C语言犄角旮旯的知识