Skip to content
Advertisement

Inserting random data into a table in MySql

Could someone please help me? I don’t know what the problem here

DROP PROCEDURE IF EXISTS insertRandom;
CREATE PROCEDURE insertRandom()
BEGIN
DECLARE mytime timestamp;

    SET mytime := '2009-01-01 00:00:00'
BEGIN
 test_loop : LOOP
    while mytime < now()
        mytime = mytime + interval '8 hours';
        insert into tempdata(temp_val, datum) values((select random()*(110)-10), mytime);
  END LOOP; 
END;

CALL insertRandom;

SELECT * FROM `temp_table`;

Error Screenshot here

Advertisement

Answer

You have many errors in your code

Use this Procedure instead

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insertRandom`()
BEGIN
DECLARE mytime timestamp;

    SET mytime := '2009-01-01 00:00:00';
DROP TEMPORARY TABLE IF EXISTS tempdata;
CREATE TEMPORARY TABLE tempdata (temp_val BIGINT, datum timestamp);
 test_loop : LOOP
    IF mytime >= now() THEN
        LEAVE  test_loop;
    END  IF;
        SET mytime = TIMESTAMPADD(HOUR,8,mytime);
        insert into tempdata(temp_val, datum) values((select (rand()*110)-10), mytime);
  END LOOP; 
END$$
DELIMITER ;

And then

call insertRandom();
SELECT * FROM tempdata;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement