Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dhakalsandeep's avatar

Loop in MySql Stored Procedures

Hi. I am new to mysql but i have previous experience in oracle. I want to loop a selection and then insert the data into another table. Is there any way to do it in mysql? As per my research i found that i could loop using a variable but everytime i have to select inside the loop to get what i want. I want to achieve something like this in mysql:

DELIMITER //

CREATE PROCEDURE insert_data()
BEGIN
	for REC in (select data_1, data_2 from table_a)
	loop
		insert into table_b 
		values(rec.data_1,rec.data_2);
	end loop;
END

DELIMITER ;
0 likes
4 replies
sr57's avatar

It's only sql

insert into tb (d1,d2) select d1,d2 from ta;
1 like
Dhakalsandeep's avatar

Inside loop there can be multiple cases depending on the selected value.

sr57's avatar

Why not ... I have better postgresql but you should find solution(s) with mysql too.

Please or to participate in this conversation.