MySQL cursor principle and usage example analysis

MySQL cursor principle and usage example analysis

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:

  • What is a cursor
  • Creating a Cursor
  • Using Cursors

Release date: 2018-04-18


What is a cursor:

  • If you have looked at mysql functions before, you will find that it is not possible to use statements that return multiple rows of results. But if you really want to use it, you need to use a cursor, which can help you select a result (so that a single result can be returned).
  • In addition, using a cursor, you can easily retrieve results that advance or go back one or more rows in the retrieved rows.
  • The cursor can iterate over multiple rows of results returned.

Replenish:

  • In MySQL, cursors are only applicable to stored procedures and functions.


Create a cursor:

  • grammar:
    • 1. Define the cursor: declare cursor name cursor for select statement;
    • 2. Open the cursor: open cursor name;
    • Get results: fetch cursor name into variable name [, variable name];
    • Close cursor: close cursor name;
      create procedure p1()
      begin
        declare id int;
        declare name varchar(15);
        --Declare cursor declare mc cursor for select * from class;
        -- Open the cursor open mc;
        -- Get the result fetch mc into id,name;
        -- Here is to display the results select id,name;
        -- Close the cursor close mc;
        
      end;
      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;


Using cursors:

  • Each time the cursor fetches, it gets a row of results. You can use variables to get the value of each column fetched.
    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 image If you keep looping, you will always reach the end of the table. When you reach the end, you can't continue fetching. Generally speaking, you should avoid errors. Before reaching the end, there will be a mysql-defined

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:
  • Definition and usage of MySQL cursor
  • Using cursor loop to read temporary table in Mysql stored procedure
  • How to declare a cursor in mysql
  • Detailed explanation of the usage and function of MySQL cursor
  • Let you thoroughly understand Python operation MySQL database (cursor explanation)
  • Detailed explanation of the principle and usage of cursor (DECLARE) in MySQL stored procedure
  • Detailed explanation of MySQL cursor concepts and usage
  • Detailed explanation of MySQL stored procedures, cursors, and transaction examples
  • Example of using cursor in Mysql stored procedure
  • Mysql stored procedure nested loop using cursor sample code
  • Example of exiting and continuing the cursor loop in MySQL stored procedures
  • MySQL cursor functions and usage

<<:  Windows Server 2012 No Remote Desktop License Server can provide a license, and the remote session is disconnected

>>:  Using Openlayer in Vue to realize loading animation effect

Recommend

Play mp3 or flash player code on the web page

Copy code The code is as follows: <object id=&...

Detailed explanation of the use of nohup /dev/null 2>&1

nohup command: If you are running a process and y...

The marquee element implements effects such as scrolling fonts and pictures

The marquee element can achieve simple font (image...

Zabbix monitors mysql instance method

1. Monitoring planning Before creating a monitori...

Steps to deploy ingress-nginx on k8s

Table of contents Preface 1. Deployment and Confi...

Notes on using $refs in Vue instances

During the development process, we often use the ...

CSS achieves colorful and smart shadow effects

background Ever wondered how to create a shadow e...

Do you know what are the ways to jump routes in Vue?

Table of contents The first method: router-link (...

In-depth reading and practice records of conditional types in TypeScript

Table of contents Using conditional types in gene...

Detailed explanation of MySQL data rows and row overflow mechanism

1. What are the formats of lines? You can see you...

Vue mobile terminal realizes finger sliding effect

This article example shares the specific code for...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...