想要让某字段,按 0000000001,0000000002,0000000003,..................这样自动增长, 用什么类型了.
    我设计的插入数据后,
  总是以整数1 开始, 如, 1,2,3................
  想请教一下, 怎么能让他按0000000001,0000000002,0000000003...................自动增长了?
      

解决方案 »

  1.   

    自增列是整型的,不要改,可以在输出结果的时候加上select right('0000000000' + cast(id as varchar(10),10)) as ID,* from tb
      

  2.   

    在学习中遇到这个问题 
    数据库里有编号字段 
    BH00001 
    BH00002 
    BH00003 
    BH00004 
    如何实现自动增长 --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号。
    --得到新编号的函数
    CREATE FUNCTION f_NextBH()
    RETURNS char(8)
    AS
    BEGIN
        RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
    END
    GO--在表中应用函数
    CREATE TABLE tb(
    BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
    col int)--插入资料
    BEGIN TRAN
        INSERT tb(col) VALUES(1)
        INSERT tb(col) VALUES(2)
        INSERT tb(col) VALUES(3)
        DELETE tb WHERE col=3
        INSERT tb(col) VALUES(4)
        INSERT tb(BH,col) VALUES(dbo.f_NextBH(),14)
    COMMIT TRAN--显示结果
    SELECT * FROM tb
    /*--结果
    BH         col 
    ---------------- ----------- 
    BH000001  1
    BH000002  2
    BH000003  4
    BH000004  14
    --*/
      

  3.   

    drop table tb
    create table tb
    (id int identity(1,1) ,
    name varchar(10),
    code as right('0000'+cast(id as varchar),5))
    go
    insert tb(name) select 'A'
    union all select 'B'
    union all select 'C'
    union all select 'D'select * from tbdrop table tb/*
    id          name       code         
    ----------- ---------- ------------ 
    1           A          00001
    2           B          00002
    3           C          00003
    4           D          00004(所影响的行数为 4 行)
    */
      

  4.   


    不是输出结果, 是写入基础表的值呀, insert 插入数据让他以00000这样开始自动增长,  为什么别人的表里可以, 而我创的表就不行了!
      

  5.   

    自增序列是INT型的,你最好就是自己写个函数了。
      

  6.   

    写了一个function,实现自动增长alter function getGrowthMaxValue()
    returns varchar(8)
    begin
    declare @max varchar(8)
    select @max=bh from t
    declare @value varchar(6)
    declare @len int
    declare @endvalue varchar(8)
    set @endvalue='BH'
    set @value=cast((cast(right(@max,6) as int)+1) as varchar(6))
    set @len=len(@value)declare @i int
    set @i=0
    while @i<(6-@len)
    begin
    set @endvalue=@endvalue+'0'
        set @i=@i+1
    end
    set @endvalue=@endvalue+@value
    return @endvalue
    end
    BH000003