upagna upagna - 5 months ago 24
SQL Question

mysql insert ip address range

I have strange situation where I would have to insert ip address in certain range , Would there be any simple way to do it instead of inserting 100s of ip address manually

example

ip address range - 33.44.0.1 to 33.44.0.254

table stucture

id, IP, timestamp, timestamp, assigned, not_assigned
1 33.44.0.1 2016-07-04 2016-07-04 0 1


Would there be any way to do it?

Answer

You can create a procedure to do this:

CREATE PROCEDURE `insert_ips`()
BEGIN

    DECLARE suffix INT DEFAULT 1;

    WHILE suffix < 255 DO
        INSERT INTO table1 VALUES (suffix, CONCAT('33.44.0.', suffix), NOW(), NOW(), 0, 1);
        SET suffix = suffix + 1;
    END WHILE;

END

And the run this procedure by CALL insert_ips().

Comments