delimiter ;;
-- 次のシーケンス
create function next_val (aid varchar(20)) returns integer
begin
insert into sequence values (aid, 1) on duplicate key update seq=seq+1;
return (select seq from sequence where id = aid);
end;;
-- シーケンス設定
create function set_val (aid varchar(20), aseq int) returns integer
begin
insert into sequence values (aid, aseq) on duplicate key update seq=aseq;
return (select seq from sequence where id = aid);
end;;
-- 現在のシーケンス
create function current_val (aid varchar(20)) returns integer
begin
return (select seq from sequence where id = aid union all select 0 limit 1);
end;;
delimiter ;