加入收藏 | 设为首页 | 会员中心 | 我要投稿 南平站长网 (https://www.0599zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

ASP自动生成编号的方法 - ASP教程

发布时间:2016-08-09 00:27:32 所属栏目:Asp教程 来源:站长网
导读:1.建种子表,这个表用来保存各个表目前已使用到的最大序号 --种子表 create table SEED ( BM varchar(20) not null, --表名 BH varchar(12) not null, --种子编
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")就是你得到的新编号。(aspcool)

(编辑:南平站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读