create table tb_sequence(name varchar(50) not null,current_value int not null,_increment int not null default 1, primary key(name));
=========================================
insert into tb_sequence values('ENUMERATE_ID',100,1);
=========================================
DELIMITER //
create function _nextval(n varchar(50)) returns integer
begin
declare _cur int;
set _cur=(select current_value from tb_sequence where name= n);
update tb_sequence
set current_value = _cur + _increment
where name=n ;
return _cur;
end;
//
=========================================
执行完毕以上几步之后,就可以执行以下sql进行验证了
select _nextval('ENUMERATE_ID');
第一次结果是100,之后每次查询都会自动加1。
每次增加多少取决于第二次的insert into tb_sequence values('ENUMERATE_ID',100,1),这个1,如果你设成2,那么每次查询之后就增加2。设置不同的name值,为不同的表业务使用。
本文介绍了如何使用SQL创建自增序列表,并通过存储过程实现序列的递增功能,便于在数据库中生成连续递增的整数,用于唯一标识符等场景。
340

被折叠的 条评论
为什么被折叠?



