As I have mentioned in my question title below Mysql function returns null always :
CREATE DEFINER=`root`@`localhost` FUNCTION `nextCode`(tbl_name VARCHAR(30), prv_code VARCHAR(30)) RETURNS varchar(30) CHARSET utf8
READS SQL DATA
BEGIN
DECLARE nxtCode VARCHAR(30);
SELECT ds.prefix, ds.suffix, ds.is_used, ds.next_number, CHAR_LENGTH(ds.pattern)
INTO @prefix, @suffix, @isUsed, @nxtNum, @pLength
FROM ths_inventory.doc_sequnce ds WHERE ds.`table_name` = tbl_name;
SET nxtCode = CONCAT(@prefix, LPAD((CASE WHEN @isUsed
THEN
(ExtractNumber(prv_code) + 1)
ELSE
(@nxtNum)
END
), @pLength,'0'), @suffix);
RETURN nxtCode;
END
But once I change the below line :
CONCAT(@prefix, LPAD((CASE WHEN @isUsed
THEN
(ExtractNumber(prv_code) + 1)
ELSE
(@nxtNum)
END
), @pLength,'0'), @suffix)
To some static values like below :
CONCAT('PR', LPAD((CASE WHEN true
THEN
(ExtractNumber(prv_code) + 1)
ELSE
(5)
END
), 6,'0'), '')
function start returning values accordingly.
Here is how I call my function :
nextCode('item','PR000002');
UPDATE:
I defined this function to get the next possible code for Item table :
According to my requirement the next possible code should be PR000000005
.
But instead of getting it, I always get empty result .
SELECT nextCode('item',(SELECT `code` FROM item ORDER BY id DESC LIMIT 1)) AS next_code;
Any help would be appreciable.