I am trying to create a user-defined function in MySQL in order to generate unique numbers. As the auto_increment feature of MySQL just increments its seed by one, I need to have such a function to handle fields that need to be incremented by more than one. Here is my SQL script:
DELIMITER $$
CREATE FUNCTION `getUniqueID`(
id_type CHAR(1)
) RETURNS INT(10)
BEGIN
DECLARE run INT(10);
START TRANSACTION;
SELECT unique_gen_id INTO @run FROM tbl_unique_seed WHERE id_type = @id_type;
UPDATE tbl_unique_seed SET unique_gen_id = (unique_gen_id + 1) WHERE id_type = @id_type;
COMMIT;
RETURN run;
END$$
DELIMITER ;
I do not have much experience in MySQL, but creating this kind of functions in SQL Server is quite easy. It will be nice of you to help me figure out a solution for this issue. Currently, I am facing some syntax errors. The most basic ones relate to the syntax of my transaction and the select statement.