欢迎光临
我们一直在努力

自动生成编号的方法

建站超值云服务器,限时71元/月

前言:

用过许多序号的方法,indentity 或 new id() ,都不好用,自己写了一个,这个序号的特点是:每次取相应表中的系统当天最大序号,如果当天无记录,则自动生成一个当天序号

1.建种子表,这个表用来保存各个表目前已使用到的最大序号

–种子表

create table seed (

bm varchar(20) not null, –表名

bh varchar(12) not null, –种子编号

constraint pk_seed primary key(bm)

)

go

2.当我们建一个新表时,同时把这个表名记录到种子表中,如:

–向种子中表添加记录

insert into seed (bm,bh) values(tablename,200211070000)

go

3.在数据库建一存储过程,自动生成新编号,此编号取当天时间,所以许多时候查询某些天的记录时,这个序号非常有用

–为参数传递来的某个表自动生成编号

if exists (select * from sysobjects where name=proc_getbh)

drop procedure proc_getbh

go

create procedure proc_getbh @bm varchar(20)

as

declare @bh char(12)

declare @today char(8)

begin

select @today=convert(char(8),getdate(),112)

select @bh=bh from seed where bm=@bm

if @bh is null or left(@bh,8)<>@today

begin

select @bh=@today+0000

end

select @bh=left(@bh,8)+ right(0000 + ltrim(convert(char(4),convert(int,right(@bh,4)))+1),4)

update seed set bh=@bh where bm=@bm

select @bh as bh

end

4.实例如下:

对表xxx自动生成新编号

set rs=conn.execute("proc_getbh @bm=xxx")

这样,rs("bh")就是你得到的新编号

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 自动生成编号的方法
分享到: 更多 (0)

相关推荐

  • 暂无文章