DROPFUNCTIONIFEXISTS nextvalForYear; DELIMITER $ CREATEFUNCTION nextvalForYear (nameVARCHAR(50)) RETURNSINTEGER LANGUAGESQL DETERMINISTIC CONTAINS SQL SQLSECURITY DEFINER COMMENT'' BEGIN set @year = (selectyearfrom seq_manage WHERE seq_name = name);
IF YEAR(NOW()) > @year THEN UPDATE seq_manage SET current_value = 1, year = YEAR(NOW()) WHERE seq_name = name; RETURN currval(name); ELSE UPDATE seq_manage SET current_value = current_value + increment WHERE seq_name = name; RETURN currval(name);
ENDIF; END $ DELIMITER ;
5. 更新当前值 setval(name,value)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
DROPFUNCTIONIFEXISTS setval; DELIMITER $ CREATEFUNCTION setval (nameVARCHAR(50), valueINTEGER) RETURNSINTEGER LANGUAGESQL DETERMINISTIC CONTAINS SQL SQLSECURITY DEFINER COMMENT'' BEGIN UPDATE seq_manage SET current_value = value WHERE seq_name = name; RETURN currval(name); END $ DELIMITER ;