SoFunction
Updated on 2025-03-03

The entire process of creating storage in mysql

MySQL creates storage

Project Scenario

MySQL's for loop executes SQL statements

Problem description

In MySQL, there is no direct FOR loop statement, but you can use loops in stored procedures to execute SQL statements.

Solution

DELIMITER $$
 
CREATE PROCEDURE InsertLoop()
BEGIN
  DECLARE v_counter INT DEFAULT 1;
 
  WHILE v_counter <= 10 DO
    INSERT INTO your_table_name(your_column_name) VALUES(v_counter);
    SET v_counter = v_counter + 1;
  END WHILE;
END$$
 
DELIMITER ;

Your_table_name and your_column_name need to be replaced with your actual table and column names.

This stored procedure inserts values ​​1 to 10 into the specified table.

To execute this stored procedure, you can use the following command:

CALL InsertLoop();

To delete this stored procedure, you can use the following command:

drop procedure InsertLoop;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.