I have a stored procedure that I'm trying to create with parameters.
DELIMITER //
CREATE PROCEDURE insert_avg(IN area INT, IN checkid INT, IN legend INT)
this_proc: BEGIN
SELECT
CASE legend
WHEN 1 THEN "Input_Day"
WHEN 2 THEN "Input_Week"
WHEN 3 THEN "Input_Month"
ELSE LEAVE this_proc
END CASE;
INTO @LEGEND;
INSERT INTO
`input_teamboards`
(
`Input_KPI`,
`Input_Month`,
`Input_Week`,
`Input_Day`,
`Input_Value`,
`Input_Comparison`,
`Input_Area`,
`Input_Created`
)
VALUES
(
7,
IFNULL(SELECT `Input_Month` FROM `input` WHERE `check_id` = checkid, NULL, MONTH(NOW())),
IFNULL(SELECT `Input_Week` FROM `input` WHERE `check_id` = checkid, NULL, WEEKOFYEAR(NOW())),
IFNULL(SELECT `Input_Day` FROM `input` WHERE `check_id` = checkid, NULL, DAYOFYEAR(NOW())),
(SELECT
AVG(`Input_value`)
FROM
`input`
WHERE
`Area_ID` = area AND @LEGEND = 1),
2,
area,
NOW()
)
END//
DELIMITER ;
As you can see the @LEGEND
variable is being used as part of a subquery for a column name further down.
The problem I'm having is with the case statement, it keeps throwing errors within the case statement:
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LEAVE this_proc END CASE; INTO @LEGEND; INSERT INTO
input_teamboards
' at line 8"
I've used these links so far, but stuck on what I'm doing wrong?
Mysql Storing a variable with the result of an SELECT CASE
Mysql - How to quit/exit from stored procedure
case statement in stored procedure
Any help is appreciated!