我想往一个表在添加一条数据记录的时候自动生成这样的编号:year+month+day+ 编号(01,02,03,04,05,06.。。)
2008030501 2008030502 2008030503 。。我准备建一个表专门来生成这个ID,这个表里永远只有一个最新的ID,怎么写啊????????

解决方案 »

  1.   

    Id, FormatId, F1 ,F2
    Id序号我设了自动加一,FormatId我想他也象这样"SL000001",
    当Insert时就加1,FormatId我想他也能自动加一"SL000001","SL000002"...
    能用一条sql什么办法实现.最好不要用中间表。有什么好方法?
    谢谢!
    create table #test
    (id int identity,
    FormatId as 'SL'+right(10000000+id,6),
    F1 varchar(50))
    go
    insert #test(F1) select '1'
    union all select '2'
    select * from #testdrop table #test
    /*
    id          FormatId       F1   
    ----------- -------------- -----
    1           SL000001       1
    2           SL000002       2(所影响的行数为 2 行)
    */
      

  2.   


    --类似 SC20080403001编号
    create table #test1 
    (id int identity,
    FormatID as 'SC'+str(year(getdate()),4)+case when month(getdate())<10  then  '0'+str(month(getdate()),1) else str(month(getdate()),2) end+
    case when day(getdate())<10 then '0'+str(day(getdate()),1) else str(day(getdate()),2) end+right(10000+id,5),
    FF varchar(50)
    )insert #test1(FF) select '1' 
    union all select '2'
    union all select '3' 
    select * from #test1 
    /*--结果如下id      FormatID     FF
    ------   ----------  -------------
    1 SC2008030510001 1
    2 SC2008030510002 2
    3 SC2008030510003 3*/
      

  3.   

    我用的是SQL SERVER 2005,如果是2000要麻烦点,好像2000里面函数里面不能用getdate()
    create table testID (id varchar(10),names varchar(10))
    go
    create function getID()
    returns char(10)
    As
    begin
        declare @id1 char(10)
        select @id1 = max(id) from testID where left(id,8) = convert(varchar(8),getdate(),112)
        if @id1 is null
           set @id1 = convert(varchar(8),getdate(),112)+'01'
        else
           select @id1 = left(@id1,8)+  right('0'+ cast(cast(right(@id1,2) as int) + 1 as varchar),2)
        return(@id1)
    end
    goinsert into testID(id,names)values(dbo.getID(),'a')
    insert into testID(id,names)values(dbo.getID(),'b')
    insert into testID(id,names)values(dbo.getID(),'c')
    insert into testID(id,names)values(dbo.getID(),'d')
    goselect * from testID--drop table testID
    --drop function getID
      

  4.   


    --以下代码生成的编号长度为12,前6位为日期信息,格式为YYMMDD,后6位为流水号。
    --创建得到当前日期的视图
    CREATE VIEW v_GetDate
    AS
    SELECT dt=CONVERT(CHAR(6),GETDATE(),12)
    GO--得到新编号的函数
    CREATE FUNCTION f_NextBH()
    RETURNS char(12)
    AS
    BEGIN
    DECLARE @dt CHAR(6)
    SELECT @dt=dt FROM v_GetDate
    RETURN(
    SELECT @dt+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) 
    FROM tb WITH(XLOCK,PAGLOCK)
    WHERE BH like @dt+'%')
    END
    GO--在表中应用函数
    CREATE TABLE tb(
    BH char(12) PRIMARY KEY DEFAULT dbo.f_NextBH(),
    col int)--插入资料
    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)--显示结果
    SELECT * FROM tb
    /*--结果
    BH           col 
    ------------------- ----------- 
    050405000001  1
    050405000002  2
    050405000003  4
    050405000004  14
    --*/