可能有一个比较笨的办法,就是设一个变量做计数器cnt
Insert Into tbl Values(xxx+CStr(cnt),...)CStr是ACCESS下的函数,我不知道MSSQL-SEVER里面是什么。

解决方案 »

  1.   

    忘记讲了,Insert 完了之后让计数器加1
      

  2.   

    问题:在实际业务处理中有些单号需要自动增长,但又不能用自增列代替
          eg:  P031106001   -- 第一位P表示采购单,031106表示日期,后三位是流水号。 /* 1:创建测试用的表结构 */
    create table tablename(pono char(10),b int)
    go
    /* 2:创建一个得到当前日期的视图,为下面的自字义函数作准备 */
    create view vGetdate
    as
      select getdate() as today
    go
    /* 3:用自定义函数来得到单号(因自定函数内不能用getdate()来得到当前日期,要用到上面的视图) */
    create function getDH()
    returns char(10)
    As
    begin
        declare @dh1 char(10),@dh2 char(10)
        select @dh1 = max(pono) from  tableName 
        Set @dh1 = IsNull(@dh1,'P000000000')
        select @dh2 = Left(@dh1,1) + right(convert(varchar(8),today,112),6) + '001' from vGetdate
        if @dh1 >= @dh2
        begin
            set @dh2 = left(@dh1,7) + right('000'+ cast(cast(right(@dh1,3) as int)+1 as varchar),3)
        end    
        return(@dh2)
    end
    go
    /* 4:在字段默认值中填入 dbo.getdh() */
    alter table tablename add constraint df_tablename_1 default(dbo.getdh()) for pono
    -/* 5:测试:*/
    insert tablename(b) values(1)
    insert tablename(b) values(2)
    Select * from tablename
    -- 测试结果
    pono       b           
    ---------- ----------- 
    P031115001 1
    P031115002 2
    总结:此方法运用到了一些小技巧
    1:用字段默认值来实现单号自增
    2:用自定义函数来得到字段的默认值
    3:因在自定义函数中不能用getdate()之类非确定的函数,用视图来得到当前日期
      

  3.   

    insert 时把最大数据传入进去,再UPDATE values = values+最大数字
      

  4.   

    --下面是自动编号处理的一些例子:--自动编号的例子.材料编号=类别编号+流水号--创建自定义函数,得到新的ID
    create function f_getid(
    @类别编号 varchar(3))
    returns int
    as
    begin
    declare @re int
    select @re=right(id,4) from(
    select id=max(材料编号) from tb where 类别编号=@类别编号
    ) a
    set @re=isnull(@re,0)+1
    return(@re)
    end
    go--创建测试表
    create table tb(材料编号 varchar(7) primary key default '',类别编号 varchar(3),材料名称 varchar(10))
    go--创建触发器,自动生成材料编号
    create trigger t_insert on tb
    instead of insert
    as
    select * into #t from inserted order by 类别编号
    declare @类别编号 varchar(3),@id int
    update #t set @id=case when @类别编号=类别编号 then @id+1 else dbo.f_getid(类别编号) end
    ,材料编号=类别编号+right('0000'+cast(@id as varchar),4)
    ,@类别编号=类别编号
    insert into tb select * from #t
    go--插入数据测试
    insert into tb(类别编号,材料名称)
    select '101','A材料'
    union all select '101','B材料'
    union all select '302','C材料'--显示结果
    select * from tb order by 材料编号go
    --删除测试环境
    drop table tb
    drop function f_getid
      

  5.   

    --自已做标识列的例子:--创建得到最大id的函数
    create function f_getid()
    returns int
    as
    begin
    declare @id int
    select @id=max(id) from tb
    set @id=isnull(@id,0)+1
    return(@id)
    end
    go--创建表
    create table tb(id int default dbo.f_getid() primary key,name varchar(10))
    go--创建触发器,在删除表中的记录时,自动更新记录的id
    create trigger t_delete on tb
    AFTER delete
    as
    declare @id int,@mid int
    select @mid=min(id),@id=@mid-1 from deleted
    update tb set id=@id,@id=@id+1 where id>@mid
    go--插入记录测试
    insert into tb(name) values('张三')
    insert into tb(name) values('张四')
    insert into tb(name) values('张五')
    insert into tb(name) values('张六')
    insert into tb(name) values('张七')
    insert into tb(name) values('张八')
    insert into tb(name) values('张九')
    insert into tb(name) values('张十')--显示插入的结果
    select * from tb--删除部分记录
    delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果
    select * from tb--删除环境
    drop table tb
    drop function f_getid