cursorA cursor is a method used to view or process data in a result set. Cursors provide the ability to navigate forward or backward through data in a result set, one or more rows at a time. How to use cursor Define a cursor: Declare cursor name CURSOR for table; (table can also be the result set of select) BEGIN #Declare custom variables declare c_stgId int; declare c_stgName varchar(50); #Declare the cursor end variable declare done INT DEFAULT 0; #Declare the cursor cr and the final processing method after the cursor reads the result set declare cr cursor for select Name,StgId from StgSummary limit 3; declare continue handler for not found set done = 1; # Open the cursor open cr; # loop readLoop: LOOP # Get the value in the cursor and assign it to the variable fetch cr into c_stgName,c_stgId; # Determine whether the cursor has reached the bottom. If so, exit the cursor. # Note this judgment IF done = 1 THEN LEAVE readLoop; END IF; SELECT c_stgName,c_stgId; END LOOP readLoop; -- Close the cursor close cr; END Note on the variable declaration statement:
Notes on custom variable naming: The name of the custom variable should not be the same as the name of the cursor result set field. If they are the same, the cursor assignment to the variable will be invalid. Temporary Tables The temporary table is only visible in the current connection. When the connection is closed, MySQL will automatically delete the table and release all space. Therefore, temporary tables with the same name can be created in different connections, and operations can be performed on the temporary tables belonging to this connection. CREATE TEMPORARY TABLE StgSummary( Name VARCHAR(50) NOT NULL, StgId INT NOT NULL DEFAULT 0 ); Restrictions on using temporary tables
ALTER TABLE orig_name RENAME new_name;
DROP TEMPORARY TABLE IF EXISTS StgTempTable; Using cursor loop to read temporary table data in stored procedureBEGIN ## Create a temporary table CREATE TEMPORARY TABLE if not exists StgSummary( Name VARCHAR(50) NOT NULL, StgId INT NOT NULL DEFAULT 0 ); TRUNCATE TABLE StgSummary; ## Add temporary table data INSERT INTO StgSummary(Name,StgId) select 'temporary data',1 BEGIN #Custom variable declare c_stgId int; declare c_stgName varchar(50); declare done INT DEFAULT 0; declare cr cursor for select Name,StgId from StgSummary ORDER BY StgId desc LIMIT 3; declare continue handler for not found set done = 1; -- Open the cursor open cr; testLoop:LOOP -- Get the result fetch cr into c_stgName,c_stgId; IF done = 1 THEN LEAVE testLoop; END IF; SELECT c_stgName,c_stgId; END LOOP testLoop; -- Close the cursor close cr; End; DROP TEMPORARY TABLE IF EXISTS StgSummary; End; Initially, create a temporary table and then define the cursor. But the stored procedure cannot be saved anyway. SummarizeWhen I wrote SQL Server stored procedures before, I didn’t pay close attention to this issue. I usually defined variables in the middle of the program, and I took MySQL for granted and wrote them casually, which eventually led to a pitfall. There is little grammatical difference between the two, but it is still quite sudden when you really encounter the difference. But I haven't written SQL statements for a long time, so I'm a little rusty. It’s better to quickly write down the pit and deepen the impression. The above is the details of using cursor loop to read temporary tables in MySQL stored procedures. For more information about MySQL cursor loop to read temporary tables, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed explanation of the role and principle of key in Vue
>>: Use docker to build kong cluster operation
Anaconda is the most popular python data science ...
1. Prepare data The following operations will be ...
ssh-secure shell, provides secure remote login. W...
Generally speaking, the background color of a web ...
Nginx: PV, UV, independent IP Everyone who makes ...
Please see the following screenshot I took from G...
Create a container [root@server1 ~]# docker run -...
Why can it set the height, but unlike elements lik...
Table of contents 1. Trigger Solution 2. Partitio...
This article will not explain the use and install...
First of all, this post is dedicated to Docker no...
Fabric.js is a very useful canvas operation plug-...
Simulation tables and data scripts Copy the follo...
This article example shares the specific code for...
Table of contents summary Problem Description Ana...