时间: 2021-07-31 作者:daque
用过很多序号的本领,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")即是你获得的新编号。