Hi I am having fun trying to get a stored procedure to parse correctly in MySQL.
My issue is with dates. I am trying to get the store procedure to create a start date that is the beginning of the current month e.g. 2009-07-01. Using this date I then use the DATA_ADD() function to add a month so that it reads 2009-08-01.
However my problem is that when I try and run the procdure to create it I get the following error:
Script line: 7 You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DECLARE
cur_month INT;
SET cur_month = (SELECT MONTH(CURRENT_DATE()));
The code for the store procedure looks like this:
DROP PROCEDURE IF EXISTS sp_test;
DELIMITER //
CREATE PROCEDURE sp_test()
BEGIN
/*we work out the start and end dates*/
DECLARE cur_year INT;
SET cur_year = (SELECT YEAR(CURRENT_DATE()));
DECLARE cur_month INT;
SET cur_month = (SELECT MONTH(CURRENT_DATE()));
DECLARE temp_date VARCHAR(10);
SET temp_date = (SELECT CONCAT(cur_year,'-',cur_month,'-01'));
DECLARE start_date DATE;
SET start_date = (SELECT CAST(temp_date AS DATE)));
DECLARE end_date DATE;
SET end_date = (SELECT DATE_ADD(start_date, INTERVAL 1 MONTH));
INSERT INTO my_table (startdate, enddate)VALUES(start_date, end_date);
END; //
DELIMITER ;
I've run the queries independantly and they all return correct values and work, it only starts to fail with syntax errors when I add them into a stored procedure.
What am I missing here that is causing all my head aches?
Thanks...