salix/db/routines/vn/functions/getNewItemId.sql

19 lines
348 B
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`getNewItemId`()
RETURNS int(11)
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE newItemId INT;
SELECT i1.id + 1 INTO newItemId
FROM item i1
LEFT JOIN item i2 ON i1.id + 1 = i2.id
WHERE i2.id IS NULL
ORDER BY i1.id
LIMIT 1;
RETURN newItemId;
END$$
DELIMITER ;