Adeptus Mechanicus
Adeptus Mechanicus

Reputation: 171

How to insert into mysql table some amount of rows with single sql request

I met a some situation, when I need insert some rows in mysql table with single sql request. With php it's simple, but I need to do this with only sql request. Is there something like "for" in mysql?

Upvotes: 0

Views: 43

Answers (1)

JohnR
JohnR

Reputation: 57

Yes you can do this may be it's helpful for you.

drop procedure if exists load_foo_test_data;

delimiter #
create procedure load_foo_test_data()
begin

declare v_max int unsigned default 1000;
declare v_counter int unsigned default 0;

    truncate table foo;
    start transaction;
    while v_counter < v_max do
        insert into foo (val) values ( floor(0 + (rand() * 65535)) );
        set v_counter=v_counter+1;
    end while;
    commit;
end #
delimiter ;

call load_foo_test_data();

select * from foo order by id;

Upvotes: 2

Related Questions