I want to pass in the string such as “1,3,5,7,9,0,3” which comes from a Java method into a function and for the function to return each value separated by the comma as an Int which will then be used later in a stored procedure (shown below).
CREATE PROCEDURE updateLastModifiedDate(IN p_classId VARCHAR(21844), IN p_timestamp VARCHAR(21844))
BEGIN
SET @query = CONCAT ('UPDATE class SET LastModifiedDate = ',p_timestamp,' WHERE ClassId IN (', p_classId ,')');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
How would I write the function?
In your case, the input to the function is a COMMA SEPARATED string. But MySql does not include a built-in function to split a COMMA SEPARATED string (comma is just an example for a separator).
You have mentioned the input as “1,3,5,7,9,0,3”. That means, the count of items is 7. This count Needs to be Constant. I can give you as example but the Count of items should be Constant always.
Also a mysql function can return a single value only. So you have to call the Procedure from within the function only (after splitting values).
Am giving you a mysql function with the assumption that, the input string contains 7 items always And the separator used was comma. You have have to modify the function otherwise.
Courtesy: