The problem was my select
query was returning more than one row, and my code was not handling them via a "loop" so it was not working and giving me errors.
This is my new Procedure...
BEGIN
DECLARE v_done INT DEFAULT FALSE; -- Variable used in the continue handler.
DECLARE dateTime datetime DEFAULT NOW();
DECLARE dateTimeCounter int DEFAULT 0;
DECLARE dateTimeCounterLimit int DEFAULT 90;
DECLARE result varchar(255) DEFAULT NULL;
DECLARE resultTimePeriod varchar(255) DEFAULT NULL;
DECLARE rsultTimeDiffH int DEFAULT NULL;
DECLARE resultUnavailiability varchar(255) DEFAULT NULL;
DECLARE resultDate date DEFAULT NULL;
DECLARE resultPossible int DEFAULT NULL;
DECLARE resultPossibleStartTime time DEFAULT NULL;
DECLARE
c CURSOR FOR SELECT CONCAT(cleaner_calender.from,
"-",
cleaner_calender.to)
AS result,
TIME_TO_SEC(
TIMEDIFF(cleaner_calender.to,
cleaner_calender.from))
AS timeDiffH,
CONCAT(cleaner_unavailability.start_time,
"-",
cleaner_unavailability.end_time)
AS unavailiability,
cleaner_unavailability.`date`,
CASE
WHEN cleaner_unavailability.start_time
IS NOT NULL
&& cleaner_unavailability.end_time
IS NOT NULL
THEN
CASE
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
&& ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) <=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
CASE
WHEN TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
cleaner_calender.from)) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
cleaner_unavailability.end_time))
THEN
TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
cleaner_calender.from))
ELSE
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
cleaner_unavailability.end_time))
END
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
&& (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
cleaner_calender.from))
WHEN (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00")))
&& ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) <=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
cleaner_unavailability.end_time))
WHEN (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00")))
&& (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
"STEP 4"
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
TIME_TO_SEC(
TIMEDIFF(cleaner_calender.to,
cleaner_calender.from))
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00")))
THEN
TIME_TO_SEC(
TIMEDIFF(cleaner_calender.to,
cleaner_calender.from))
ELSE
"STEP LAST"
END
ELSE
TIME_TO_SEC(
TIMEDIFF(cleaner_calender.to,
cleaner_calender.from))
END
AS possible,
CASE
WHEN cleaner_unavailability.start_time
IS NOT NULL
&& cleaner_unavailability.end_time
IS NOT NULL
THEN
CASE
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
&& ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) <=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
CASE
WHEN TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
cleaner_calender.from)) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
cleaner_unavailability.end_time))
THEN
cleaner_calender.from
ELSE
cleaner_unavailability.end_time
END
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
&& (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
cleaner_calender.from
WHEN (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00")))
&& ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) <=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
cleaner_unavailability.end_time
WHEN (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00")))
&& (TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
0
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) >=
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.to,
"00:00:00")))
THEN
cleaner_calender.from
WHEN ( TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.start_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00"))
&& TIME_TO_SEC(
TIMEDIFF(
cleaner_unavailability.end_time,
"00:00:00")) <
TIME_TO_SEC(
TIMEDIFF(
cleaner_calender.from,
"00:00:00")))
THEN
cleaner_calender.from
ELSE
0
END
ELSE
cleaner_calender.from
END
AS possibleStartTime
FROM cleaner_calender
LEFT JOIN cleaner_unavailability
ON cleaner_calender.cleaner_id =
cleaner_unavailability.cleaner_id
AND cleaner_unavailability.status = "1"
AND cleaner_unavailability.`date` = DATE(dateTime)
WHERE cleaner_calender.status = "1"
AND day = DAYNAME(dateTime)
HAVING possible > seconds;
-- Declaring continue handler for the cursor stg_product_data
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done=TRUE;
DROP TABLE IF EXISTS foo;
CREATE TABLE foo
(
id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
date1 date NULL,
dayNameP varchar(20) NULL,
timings varchar(255) NULL,
tresultTimePeriod varchar(255) NULL,
trsultTimeDiffH varchar(255) NULL,
tresultUnavailiability varchar(255) NULL,
tresultDate varchar(255) NULL,
tresultPossible varchar(255) NULL,
tresultPossibleStartTime varchar(255) NULL
)
ENGINE = innodb;
WHILE dateTimeCounter < dateTimeCounterLimit
DO
OPEN c;
read_loop:
LOOP -- Loop through the records
FETCH c
INTO resultTimePeriod,
rsultTimeDiffH,
resultUnavailiability,
resultDate,
resultPossible,
resultPossibleStartTime;
IF v_done
THEN -- Checking if the cursor has fetched the last record
LEAVE read_loop; -- Exit if last record had been fetched
ELSE
-- IF resultPossible IS NOT NULL
-- THEN
INSERT INTO foo(date1, dayNameP, timings,
tresultTimePeriod, trsultTimeDiffH,
tresultUnavailiability, tresultDate, tresultPossible,
tresultPossibleStartTime)
VALUES (dateTime, DAYNAME(dateTime), resultPossible,
resultTimePeriod, rsultTimeDiffH,
resultUnavailiability, resultDate, resultPossible,
resultPossibleStartTime);
-- END IF;
END IF;
END LOOP;
SET dateTime = DATE_ADD(dateTime, INTERVAL 1 DAY);
SET dateTimeCounter = dateTimeCounter + 1;
SET v_done = FALSE ;
CLOSE c;
END WHILE;
SELECT * FROM foo;
END
cleaner_calender
CREATE TABLE
statement, some rows and your Stored Procedure parameters, for test purposes? – oNare Jul 14 '15 at 13:26FETCH
if you're only taking 1 row with a concatenating string of '00:30:00-02:00:00,00:30:00-02:00:00,00:30:00-02:00:00', what do you want to achieve? Can you add yourfoo
expected output? – oNare Jul 14 '15 at 14:19