How to create a stored procedure in MySQL and add records in a loop

How to create a stored procedure in MySQL and add records in a loop

This article uses an example to describe how to create a stored procedure in MySQL and add records in a loop. Share with you for your reference, the details are as follows:

First create, then call:

-- Create stored procedure DELIMITER; //
create procedure myproc()
begin
declare num int;
set num=1;
while num <= 24 do
insert into t_calendar_hour(hourlist) values(num);
set num=num+1;
end while;
commit;
end;//
-- Call the stored procedure CALL myproc();

PS: Here are two commonly used SQL online tools for your reference (including SQL statement usage instructions):

SQL online compression/formatting tool:
http://tools.jb51.net/code/sql_format_compress

SQL code online formatting and beautification tool:
http://tools.jb51.net/code/sqlcodeformat

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL stored procedure skills", "MySQL common function summary", "MySQL log operation skills", "MySQL transaction operation skills summary" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Simple writing of MYSQL stored procedures and functions
  • Detailed example of MySQL data storage process parameters
  • Two ways to write stored procedures in Mysql with and without return values
  • Detailed explanation of MySQL stored procedures, cursors, and transaction examples
  • Detailed explanation of the entry-level use of MySql stored procedure parameters
  • mysql uses stored procedures to implement tree node acquisition method

<<:  Example of how to reference environment variables in Docker Compose

>>:  Fabric.js implements DIY postcard function

Recommend

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

MySQL 8.0.21.0 Community Edition Installation Tutorial (Detailed Illustrations)

1. Download MySQL Log in to the MySQL official we...

Analysis of Linux configuration to achieve key-free login process

1.ssh command In Linux, you can log in to another...

Thirty HTML coding guidelines for beginners

1. Always close HTML tags In the source code of p...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

MySQL trigger definition and usage simple example

This article describes the definition and usage o...

How to use jconsole to monitor remote Tomcat services

What is JConsole JConsole was introduced in Java ...