I would like to insert timestamp values of 3 minute steps (given a start and end date) into a table with just one timestamp column.
I currently switch from sql server to mysql. I would know how to do it for the former database in SQL but am not sure about the latter. Thanks.
This is my current (not working) miserable attempt:
DELIMITER //
CREATE PROCEDURE test()
begin
declare StartTS TIMESTAMP DEFAULT null;
declare EndTS TIMESTAMP DEFAULT null;
set StartTS = '2012-01-01 00:00:00';
set EndTS = '2014-01-01 00:00:00';
start transaction;
while StartTS < EndTS do
insert into timestamps (TimeStampEntry) values (StartTS);
SET StartTS = StartTS + INTERVAL 3 MINUTE;
end while;
commit;
end
//
DELIMITER ;
call test();
PS:
create table TimeStamps (
Id INTEGER NOT NULL AUTO_INCREMENT,
TimeStampEntry DATETIME,
primary key (Id)
)
current sproc:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `myProc`(IN t1 timestamp, IN t2 timestamp)
BEGIN
WHILE t1 <= t2 DO
INSERT INTO timestamps (TimeStampEntry) Values (t1) ;
SET t1 = DATE_ADD(t1, INTERVAL 180 SECOND);
#SET t1 = t1 + INTERVAL 180 SECOND;
END WHILE;
END
call:
CALL myProc('2010-01-01 00:00:00', '2014-01-01 00:00:00');
try something like this:
http://sqlfiddle.com/#!2/5672e/9
Or change your line:
to