当前位置:网站首页>Use the fetch statement to obtain the repetition of the last row of cursor data
Use the fetch statement to obtain the repetition of the last row of cursor data
2022-07-07 10:22:00 【qq_ forty-two million one hundred and twenty thousand eight hun】
- Problem description :《MySQL Will know 》 Medium P179 After the execution of the stored procedure, a row is repeatedly inserted in the new table .
- stored procedure processorders( To calculate the orders The total amount of each order in the table and deposit in a new table )
# Create a stored procedure to calculate the total amount of each order and store it in a new table
DELIMITER //
CREATE PROCEDURE processorders()
BEGIN
# Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
# declare cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
# Declare exception handling , When sql Status as 02000 when , take done Set to 1
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
# Create a new table to store the results
CREATE TABLE IF NOT EXISTS ordertotals
(order_num INT,
total DECIMAL(8,2)
);
# Open cursor
OPEN ordernumbers;
# Insert all rows in a loop
REPEAT
# Get the order number , When the intra row pointer points to the next row of the last row, executing this statement will make SQLSTATE='02000'
# Cause the execution of the upper handle processing to make done = 1
FETCH ordernumbers INTO o;
# Calculate the total amount with tax , The result will be returned to the parameter t, The stored procedure is at the bottom
CALL ordertotal(o,1,t);
# Insert the order number and total amount into the table
INSERT INTO ordertotals(order_num,total)
VALUES(o,t);
UNTIL done # The end cycle condition is done For the wrong 0
END REPEAT;
# Close cursor
CLOSE ordernumbers;
END//
# Execute query statement
CALL processorders()//
- The result

- Problem analysis : When the pointer in the cursor points to the next row of the last row , Re execution
FETCH ordernumbers INTO o;When this statement , To take the current line and move the pointer down one line , Because the position pointed by the pointer is illegal , So there is a row error , Cause the upper handle processing to trigger , bring done = 1, And then o No new data is inserted , Or the old data of the last round ( That is, the order number in the last line ), Therefore, the last line is repeatedly inserted twice .
resolvent : stay FETCH Judge immediately after the statement is executed done Is it modified to 1, If it is modified to 1, Then the following insert code will not be executed . The stored procedure code above is modified to
DELIMITER //
CREATE PROCEDURE processorders()
BEGIN
# Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
# declare cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
# Declare exception handling , When sql Status as 02000 when , take done Set to 1
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
# Create a new table to store the results
CREATE TABLE IF NOT EXISTS ordertotals
(order_num INT,
total DECIMAL(8,2)
);
# Open cursor
OPEN ordernumbers;
# Insert all rows in a loop
REPEAT
# Get the order number , When the intra row pointer points to the next row of the last row, executing this statement will make SQLSTATE='02000'
# Cause the execution of the upper handle processing to make done = 1
FETCH ordernumbers INTO o;
IF !done THEN # The key position of the correction
# Calculate the total amount with tax , The result will be returned to the parameter t, The stored procedure is at the bottom
CALL ordertotal(o,1,t);
# Insert the order number and total amount into the table
INSERT INTO ordertotals(order_num,total)
VALUES(o,t);
END IF;
UNTIL done
END REPEAT;
# Close cursor
CLOSE ordernumbers;
END//Query again ordertotals The result of the table is

Reference material :
边栏推荐
- request对象对请求体,请求头参数的解析
- Postman interface test IV
- Memory ==c language 1
- STM32基础知识—内存映射
- The physical meaning of imaginary number J
- ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
- 【acwing】789. 数的范围(二分基础)
- Fiddler break point
- ORM -- grouping query, aggregation query, query set queryset object properties
- Introduction to energy Router: Architecture and functions for energy Internet
猜你喜欢

【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法

ORM -- database addition, deletion, modification and query operation logic

【acwing】786. Number k

【acwing】789. 数的范围(二分基础)

STM32 ADC and DMA

JMeter loop controller and CSV data file settings are used together

Embedded background - chip

SolidWorks工程图中添加中心线和中心符号线的办法
![[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet](/img/3c/890f2481231482645554f86dd614a9.png)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet

ORM -- grouping query, aggregation query, query set queryset object properties
随机推荐
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
[second on] [jeecgboot] modify paging parameters
Easyexcel read write simple to use
STM32中AHB总线_APB2总线_APB1总线这些是什么
IPv4套接字地址结构
Learning records - high precision addition and multiplication
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
【剑指Offer】42. 栈的压入、弹出序列
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Programming features of ISP, IAP, ICP, JTAG and SWD
【二开】【JeecgBoot】修改分页参数
【华为机试真题详解】高矮个子排队
IPv4 socket address structure
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
Appx代码签名指南
虚数j的物理意义
ORM模型--数据记录的创建操作,查询操作
Win10 installation vs2015
Postman interface test VII