0

Hello All.
I'm trying to loop the following statement with PHPmyAdmin in order to add 1000 entries with the variable "i" incremented for each entry. (data and ip can be static)

INSERT INTO 'TABLE'('id', 'data', 'ip') VALUES ([i], [06/03/2016],[192.168.0.1])

It's been 20 years since I've done any coding with loops. Can anyone help me?

2
Contributors
3
Replies
20
Views
1 Year
Discussion Span
Last Post by AndrisP
0
CREATE TABLE `test_table`(
    `id` INT NOT NULL AUTO_INCREMENT,
    `data` DATE,
    `ip` VARCHAR(30),
    PRIMARY KEY (`id`)
);

DELIMITER $$
DROP PROCEDURE IF EXISTS insert_data; $$
CREATE PROCEDURE insert_data(IN p_cur INT, IN p_max INT)
BEGIN
    TRUNCATE TABLE `test_table`;
    START TRANSACTION;
    WHILE p_cur <= p_max do
        INSERT INTO `test_table`(`id`, `data`,`ip`) VALUES (p_cur, NOW(), '192.168.0.1');
        SET p_cur = p_cur + 1;
    END WHILE;
    COMMIT;
END; $$
DELIMITER ;
CALL insert_data(1,1000);

Edited by AndrisP

0

Thanks alot! With some slight modification, this works PERFECTLY!

Great Community!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.