ASP自动生成编号的方法 [ASP]

2013 年 11 月 26 日4680

您现在的位置:网络学院>开发特区>Web开发>ASP

ASP自动生成编号的方法

出处:PConline
责任编辑:pcedu

[03-9-8 16:13]

作者:张十一

-

  用过许多序号的方法,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)

  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")就是你得到的新编号。(aspcool)


[1]

ad

0 0