如何实现有一组有规则的编号(如200305310001…200305310999)

解决方案 »

  1.   

    declare @i int
    set @i = 1
    while @i < 1000
    print convert(varchar(8),getdate(),112)+right(10000+@i,4)
      

  2.   

    --下面的代码生成长度为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.   

    select 200305310001+number
    from master..spt_values
    where type='p' and number<999/*
    ---------------------------------------
    200305310001
    200305310002
    200305310003
    200305310004
    200305310005
    200305310006
    200305310007
    200305310008
    ..
      

  4.   

    +1declare @i int
    set @i = 1
    while @i < 1000
    begin
    print convert(varchar(8),getdate(),112)+right(10000+@i,4)
    set @i=@i+1
    end