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

19 lines
350 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`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 ;