This article uses examples to explain the principles and usage of MySQL cursors. Share with you for your reference, the details are as follows: In this article:
Release date: 2018-04-18 What is a cursor:
Replenish:
Create a cursor:
Using cursors:
create procedure p2() begin declare id int; declare name varchar(15); --Declare cursor declare mc cursor for select * from class; -- Open the cursor open mc; -- Get result loop -- Loop and transfer the contents of the table to class2 fetch mc into id,name; -- Here is to display the result insert into class2 values(id,name); -- Close the cursor end loop; close mc; end; The above code will have an error create procedure p3() begin declare id int; declare name varchar(15); declare flag int default 0; --Declare cursor declare mc cursor for select * from class; declare continue handler for not found set flag = 1; -- Open the cursor open mc; -- Get the result l2:loop fetch mc into id,name; if flag=1 then -- when fetch fails, handler continues leave l2; end if; -- Here is to display the result insert into class2 values(id,name); -- Close the cursor end loop; close mc; end; call p3();-- no error select * from class2; Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
>>: Using Openlayer in Vue to realize loading animation effect
The scroll bar position is retained when scrollin...
Nginx uses a fixed number of multi-process models...
In MySQL, the LOAD_FILE() function reads a file a...
This article describes how to build a MySQL maste...
1. Download MySQL Community Server 5.7.16 and ins...
Promise is a new solution for asynchronous progra...
There are two ways to expose container ports in d...
MySQL installation tutorial for Windows system do...
Table of contents 1. What is dynamic typing? 2. D...
Data display has always been a demand that all wa...
1. Add a comment block at the beginning of the sty...
Table of contents 1. Introduction II. Monitoring ...
The props of the component (props is an object) F...
bgcolor="text color" background="ba...
The installation tutorial of mysql 8.0.11 winx64 ...