How can I loop through values and insert them into a database. Specifically what I am doing is this: I have a years table that I want to look something like this:
years
id name
1 2015*
2 2014
3 2013
etc etc
(2015 is used here because this question was made in 2014, and I want to make the first field the next year of whatever the current year is - eg - if you're reading this question in 2020, the first row should be 2021).
The id field is auto_incremented, and I have tried this after some searching around...
CREATE PROCEDURE `vehicle_years_data()`
BEGIN
DECLARE i INT DEFAULT YEAR(CURDATE()) + 1;
WHILE (i >= 1900) DO
INSERT INTO `vehicle_years` (`name`) VALUE(CONVERT(i, VARCHAR(255));
SET i = i - 1;
END WHILE
END$$
DELIMITER ;
CALL `vehicle_years_data()`;
DROP PROCEDURE `vehicle_years_data()`;
But that gives me:
You have an error in your SQL syntax; check the
BEGIN DECLARE i INT DEFAULT YEAR(CUR' at line 10
I know the static way to do this is to simply to
INSERT INTO `vehicle_years` (`name`) VALUES
(2015), (2014), (2013), ...
But that is not only more tedious, but I can't make it dynamically start with the current year plus one that way.
Any help would be much appreciated.