在学习中遇到这个问题 
数据库里有编号字段 
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
--*/ create table tb
(id int identity,
name varchar(10),
code as 'BH'+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          BH00001
2           B          BH00002
3           C          BH00003
4           D          BH00004(所影响的行数为 4 行)
*/

解决方案 »

  1.   


    china+right('00000'+cast(cast(substring(max(col),6,5)as int)+1 as char),5)
      

  2.   


    'china'+right('00000'+cast(cast(substring(max(col),6,5)as int)+1 as char),5)
      

  3.   

    谢谢,如果表中已经有了一些记录,比如最大值是china10864,我想从 china12000开始,以后每次追加一条记录,就从 china12000开始加1,该怎么做呢?当然表中原有的记录保持不变
      

  4.   

    -->创建表
    if object_id('tb') is not null
      drop table tb
    go
    create table tb([id] char(10),[date] datetime)
    insert tb select 'china10001','2009-04-01 20:10:10'
    insert tb select 'china10002','2009-03-30 19:05:05'
    insert tb select 'china10003','2009-03-25 06:05:01'
    insert tb select 'china10005','2009-03-10 12:05:05'
    insert tb select 'china10007','2009-03-30 06:55:01'
    go--创建函数
    create FUNCTION f_NextBH()
    RETURNS char(10)
    AS
    BEGIN
        RETURN(SELECT 'china'+RIGHT(100001+ISNULL(RIGHT(MAX(id),5),0),5) FROM tb WITH(XLOCK,PAGLOCK))
    END
    GO
    -->更新例默认值
    alter table tb add constraint df_id default(dbo.f_NextBH()) for id
    --alter table tb drop constraint df_id
    -->插入测试数据
    insert tb(date) select '2009-03-31 06:55:01'
    -->查询
    select * from tb
    /*
    id         date
    ---------- -----------------------
    china10001 2009-04-01 20:10:10.000
    china10002 2009-03-30 19:05:05.000
    china10003 2009-03-25 06:05:01.000
    china10005 2009-03-10 12:05:05.000
    china10007 2009-03-30 06:55:01.000
    china10008 2009-03-31 06:55:01.000(6 行受影响)*/
      

  5.   

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

  6.   

    -->创建表
    if object_id('tb') is not null
      drop table tb
    go
    create table tb([id] char(10),[date] datetime)
    insert tb select 'china10001','2009-04-01 20:10:10'
    insert tb select 'china10002','2009-03-30 19:05:05'
    insert tb select 'china10003','2009-03-25 06:05:01'
    insert tb select 'china10005','2009-03-10 12:05:05'
    insert tb select 'china10007','2009-03-30 06:55:01'
    go--创建函数
    create FUNCTION f_NextBH()
    RETURNS char(10)
    AS
    BEGIN
        RETURN(SELECT 'china'+RIGHT(100001+ISNULL(RIGHT(MAX(id),5),0),5) FROM tb WITH(XLOCK,PAGLOCK))
    END
    GO
    -->更新例默认值
    alter table tb add constraint df_id default(dbo.f_NextBH()) for id
    --alter table tb drop constraint df_id
    -->插入测试数据
    insert tb select 'china12000','2009-03-31 06:55:01' --从12000开始
    insert tb(date) select '2009-03-31 06:55:01'
    -->查询
    select * from tb
    /*
    id         date
    ---------- -----------------------
    china10001 2009-04-01 20:10:10.000
    china10002 2009-03-30 19:05:05.000
    china10003 2009-03-25 06:05:01.000
    china10005 2009-03-10 12:05:05.000
    china10007 2009-03-30 06:55:01.000
    china10008 2009-03-31 06:55:01.000
    china12000 2009-03-31 06:55:01.000
    china12001 2009-03-31 06:55:01.000(8 行受影响)
    */