热闹 热闹 标签 标签

mysql 函数创建

huwhy
评论(0) 浏览(831)

delimiter // CREATE DEFINER=root@% FUNCTION nextval(seq_name varchar(100)) RETURNS bigint(20) SQL SECURITY INVOKER BEGIN DECLARE cur_val bigint(20); SELECT current_value + increment INTO cur_val FROM sequence WHERE name = seq_name for update;

IF cur_val IS NOT NULL THEN
  UPDATE sequence SET current_value = cur_val WHERE `name` = seq_name;
ELSE
  insert into sequence(`name`, current_value, increment) values (seq_name, 1, 1);
  select 1 into cur_val;
END IF;

RETURN cur_val;

END // delimiter ;

0